获取远程网页的内容之二(downmoon原创)
程序员文章站
2023-11-24 18:32:10
本文仅针ad下代理上网的情况: 代码如下: 1、定义变量: 定义变量#region 定义变量 复制代码 代码如下:private ...
本文仅针ad下代理上网的情况:
代码如下:
1、定义变量:
定义变量#region 定义变量
private string strfirewallip
...{
get
...{
return system.configuration.configurationsettings.appsettings["strfirewallip"];
}
}
private string strfirewallport
...{
get
...{
return system.configuration.configurationsettings.appsettings["strfirewallport"];
}
}
private string struid
...{
get
...{
return system.configuration.configurationsettings.appsettings["struid"];
}
}
private string strpwd
...{
get
...{
return system.configuration.configurationsettings.appsettings["strpwd"];
}
}
private string strdomain
...{
get
...{
return system.configuration.configurationsettings.appsettings["strdomain"];
}
}
#endregion
方法:
获取指定远程网页内容
/**//// <summary>
/// 获取指定远程网页内容
/// </summary>
/// <param name="strurl">所要查找的远程网页地址</param>
/// <returns></returns>
//[webmethod(description = "获取指定远程网页内容。")]
public string getpagecontent(string strurl)
...{
string strresult = "";
this.currenturl = strurl;
if(this.currenturl.tolower().startswith("http://")==false)
this.currenturl = "http://"+this.currenturl;
try
...{
contentbytes = gethtmlbyte(currenturl);
}
catch(exception err)
...{
strresult = "请求错误:" + err.message;
}
if(contentbytes==null)
...{
throw new exception("没有获得返回值");
}
strresult = getstringfrombytearray(contentbytes,encoding.utf8);
return strresult;
}
获取指定远程网页元素字节数组::
获取指定远程网页元素字节数组#region 获取指定远程网页元素字节数组
/**//// <summary>
/// 获取指定远程网页元素字节数组
/// </summary>
/// <param name="strurl">所要查找的远程网页地址</param>
/// <returns></returns>
private byte[] gethtmlbyte(string strurl)
...{
string strpara=(strurl.indexof("?")>=0?strurl.substring(strurl.indexof("?")+1):"");
system.text.encoding encoding = new utf8encoding();
byte[] byte1 = encoding.getbytes(strpara);
byte[] bytereturn = new byte[10000000];
if(strurl.trim().tolower().startswith("http://")==false)
...{
strurl = "http://"+strurl;
}
httpwebrequest myhttpwebrequest = (httpwebrequest)webrequest.create(strurl);
myhttpwebrequest.allowautoredirect = true;
myhttpwebrequest.keepalive = true;
myhttpwebrequest.useragent = "mozilla/4.0 (compatible; msie 6.0; windows nt 5.0; .net clr 1.1.4322)";
system.net .webproxy proxy = new webproxy(strfirewallip+":"+strfirewallport,true);
//proxy=(webproxy)system.net.globalproxyselection.select;
system.net.networkcredential mycredential = new networkcredential(struid,strpwd,strdomain);
proxy.credentials =mycredential;
myhttpwebrequest.proxy = proxy;
httpwebresponse myhttpwebresponse = (httpwebresponse)myhttpwebrequest.getresponse();
byte[] bread = new byte[1024];
int lngcount = 1;
int totallen = 0;
stream recweb = myhttpwebresponse.getresponsestream();
lngcount = recweb.read(bread,0,1024);
while(lngcount>0)
...{
array.copy(bread,0,bytereturn,totallen,lngcount);
totallen += lngcount;
lngcount = recweb.read(bread,0,1024);
}
recweb.close();
byte[] bytegets = new byte[totallen];
array.copy(bytereturn,0,bytegets,0,totallen);
bytereturn = null;
bread = null;
return bytegets;
}
#endregion
转换指定字节数组为字符串::
转换指定字节数组为字符串#region 转换指定字节数组为字符串
/**//// <summary>
/// 转换指定字节数组为字符串
/// </summary>
/// <param name="byteget">字节数组byte[]</param>
/// <param name="myencoding">编码方式</param>
/// <returns></returns>
private static string getstringfrombytearray(byte[] byteget,encoding myencoding)
...{
int i,lngcount;
stringbuilder atemp = new stringbuilder(10000);
lngcount = byteget.length;
for(i = 0;i<lngcount;i+= 10000)
...{
atemp.append(myencoding.getstring(byteget,i,(lngcount>=i+10000?10000:lngcount - i)));
}
if(i<=lngcount)
...{
atemp.append(myencoding.getstring(byteget,i,(lngcount - i)));
}
return atemp.tostring();
}
#endregion
借用这个,写了个抽取中国天气网预报的服务!很爽!
代码如下:
1、定义变量:
定义变量#region 定义变量
复制代码 代码如下:
private string strfirewallip
...{
get
...{
return system.configuration.configurationsettings.appsettings["strfirewallip"];
}
}
private string strfirewallport
...{
get
...{
return system.configuration.configurationsettings.appsettings["strfirewallport"];
}
}
private string struid
...{
get
...{
return system.configuration.configurationsettings.appsettings["struid"];
}
}
private string strpwd
...{
get
...{
return system.configuration.configurationsettings.appsettings["strpwd"];
}
}
private string strdomain
...{
get
...{
return system.configuration.configurationsettings.appsettings["strdomain"];
}
}
#endregion
方法:
获取指定远程网页内容
复制代码 代码如下:
/**//// <summary>
/// 获取指定远程网页内容
/// </summary>
/// <param name="strurl">所要查找的远程网页地址</param>
/// <returns></returns>
//[webmethod(description = "获取指定远程网页内容。")]
public string getpagecontent(string strurl)
...{
string strresult = "";
this.currenturl = strurl;
if(this.currenturl.tolower().startswith("http://")==false)
this.currenturl = "http://"+this.currenturl;
try
...{
contentbytes = gethtmlbyte(currenturl);
}
catch(exception err)
...{
strresult = "请求错误:" + err.message;
}
if(contentbytes==null)
...{
throw new exception("没有获得返回值");
}
strresult = getstringfrombytearray(contentbytes,encoding.utf8);
return strresult;
}
获取指定远程网页元素字节数组::
获取指定远程网页元素字节数组#region 获取指定远程网页元素字节数组
复制代码 代码如下:
/**//// <summary>
/// 获取指定远程网页元素字节数组
/// </summary>
/// <param name="strurl">所要查找的远程网页地址</param>
/// <returns></returns>
private byte[] gethtmlbyte(string strurl)
...{
string strpara=(strurl.indexof("?")>=0?strurl.substring(strurl.indexof("?")+1):"");
system.text.encoding encoding = new utf8encoding();
byte[] byte1 = encoding.getbytes(strpara);
byte[] bytereturn = new byte[10000000];
if(strurl.trim().tolower().startswith("http://")==false)
...{
strurl = "http://"+strurl;
}
httpwebrequest myhttpwebrequest = (httpwebrequest)webrequest.create(strurl);
myhttpwebrequest.allowautoredirect = true;
myhttpwebrequest.keepalive = true;
myhttpwebrequest.useragent = "mozilla/4.0 (compatible; msie 6.0; windows nt 5.0; .net clr 1.1.4322)";
system.net .webproxy proxy = new webproxy(strfirewallip+":"+strfirewallport,true);
//proxy=(webproxy)system.net.globalproxyselection.select;
system.net.networkcredential mycredential = new networkcredential(struid,strpwd,strdomain);
proxy.credentials =mycredential;
myhttpwebrequest.proxy = proxy;
httpwebresponse myhttpwebresponse = (httpwebresponse)myhttpwebrequest.getresponse();
byte[] bread = new byte[1024];
int lngcount = 1;
int totallen = 0;
stream recweb = myhttpwebresponse.getresponsestream();
lngcount = recweb.read(bread,0,1024);
while(lngcount>0)
...{
array.copy(bread,0,bytereturn,totallen,lngcount);
totallen += lngcount;
lngcount = recweb.read(bread,0,1024);
}
recweb.close();
byte[] bytegets = new byte[totallen];
array.copy(bytereturn,0,bytegets,0,totallen);
bytereturn = null;
bread = null;
return bytegets;
}
#endregion
转换指定字节数组为字符串::
转换指定字节数组为字符串#region 转换指定字节数组为字符串
复制代码 代码如下:
/**//// <summary>
/// 转换指定字节数组为字符串
/// </summary>
/// <param name="byteget">字节数组byte[]</param>
/// <param name="myencoding">编码方式</param>
/// <returns></returns>
private static string getstringfrombytearray(byte[] byteget,encoding myencoding)
...{
int i,lngcount;
stringbuilder atemp = new stringbuilder(10000);
lngcount = byteget.length;
for(i = 0;i<lngcount;i+= 10000)
...{
atemp.append(myencoding.getstring(byteget,i,(lngcount>=i+10000?10000:lngcount - i)));
}
if(i<=lngcount)
...{
atemp.append(myencoding.getstring(byteget,i,(lngcount - i)));
}
return atemp.tostring();
}
#endregion
借用这个,写了个抽取中国天气网预报的服务!很爽!
上一篇: Python装饰器使用示例及实际应用例子
下一篇: 在ASP.NET中实现多文件上传的方法