获取远程网页的内容之一(downmoon原创)
程序员文章站
2022-10-12 19:47:01
获取远程网页的内容之一(downmoon原创) 一、本机直接上网时: 获取指定远程网页内容#region 获取指定远程网页内容 ...
获取远程网页的内容之一(downmoon原创)
一、本机直接上网时:
获取指定远程网页内容#region 获取指定远程网页内容
/**//// <summary>
/// 获取指定远程网页内容
/// </summary>
/// <param name="strurl">所要查找的远程网页地址</param>
/// <param name="timeout">超时时长设置,一般设置为8000</param>
/// <param name="entertype">是否输出换行符,0不输出,1输出文本框换行</param>
/// <param name="encodetype">编码方式</param>
/// <returns></returns>
/// 也可考虑 static string
public string getrequeststring(string strurl,int timeout,int entertype,encoding encodetype)
...{
string strresult;
try
...{
httpwebrequest myreq = (httpwebrequest)httpwebrequest.create(strurl) ;
myreq.timeout = timeout;
httpwebresponse httpwresp = (httpwebresponse)myreq.getresponse();
stream mystream = httpwresp.getresponsestream () ;
streamreader sr = new streamreader(mystream , encodetype);
stringbuilder strbuilder = new stringbuilder();
while (-1 != sr.peek())
...{
strbuilder.append(sr.readline());
if(entertype==1)
...{
strbuilder.append(" ");
}
}
strresult = strbuilder.tostring();
}
catch(exception err)
...{
strresult = "请求错误:" + err.message;
}
return strresult ;
}
#endregion
二:通过域环境代理上网时这样就不行了! 下篇文章分解,呵呵
一、本机直接上网时:
获取指定远程网页内容#region 获取指定远程网页内容
复制代码 代码如下:
/**//// <summary>
/// 获取指定远程网页内容
/// </summary>
/// <param name="strurl">所要查找的远程网页地址</param>
/// <param name="timeout">超时时长设置,一般设置为8000</param>
/// <param name="entertype">是否输出换行符,0不输出,1输出文本框换行</param>
/// <param name="encodetype">编码方式</param>
/// <returns></returns>
/// 也可考虑 static string
public string getrequeststring(string strurl,int timeout,int entertype,encoding encodetype)
...{
string strresult;
try
...{
httpwebrequest myreq = (httpwebrequest)httpwebrequest.create(strurl) ;
myreq.timeout = timeout;
httpwebresponse httpwresp = (httpwebresponse)myreq.getresponse();
stream mystream = httpwresp.getresponsestream () ;
streamreader sr = new streamreader(mystream , encodetype);
stringbuilder strbuilder = new stringbuilder();
while (-1 != sr.peek())
...{
strbuilder.append(sr.readline());
if(entertype==1)
...{
strbuilder.append(" ");
}
}
strresult = strbuilder.tostring();
}
catch(exception err)
...{
strresult = "请求错误:" + err.message;
}
return strresult ;
}
#endregion
上一篇: 公司真善解人意
下一篇: 结婚后生个孩子叫什么呢