admin 管理员组文章数量: 887021
2023年12月22日发(作者:前端css3新特性)
方法一:
using ;
using ;
private string getHtml(string url)
{
WebClient myWebClient = new WebClient();
byte[] myDataBuffer = adData (url);
return ing(myDataBuffer);
}
方法二:
public string getHttp(string HttpUrl,string RefererUrl)
{
string html = "";
try
{
P Http = new PClass();
("GET", Url, false, null, null);
("GET",HttpUrl,false,null,null);
uestHeader("Referer",RefererUrl);
//uestHeader("Referer",RefererUrl);
uestHeader("Content-Type", "text/html;charset=gb2312");
("");
html = ing((byte[])seBody);
Http = null;
}
catch
{
}
return html;
}
public bool getweb(string strURL,out string buf)
{
buf="";
try
{
//Uri url=new Uri(strURL,false);
HttpWebRequest request;
request = (HttpWebRequest)(strURL);
="POST"; //Post请求方式
tType="text/html;charset=gb2312"; //内容类型
string paraUrlCoded = ode(""); //参数经过URL编码
byte[] payload;
payload = oding("GB2312").GetBytes(paraUrlCoded); //将URL编码后的字符串转化为字节
tLength = ; //设置请求的ContentLength
Stream writer = uestStream(); //获得请求流
(payload,0,); //将请求参数写入流
(); //关闭请求流
HttpWebResponse response;
response = (HttpWebResponse)ponse(); //获得响应流
Stream s;
s = ponseStream();
StreamReader objReader = new StreamReader(s,
oding("GB2312"));
string HTML = "";
string sLine ="";
int i = 0;
while (sLine!=null)
{
i++;
sLine = ne();
if (sLine!=null)
HTML += sLine;
}
//HTML = e("<","<");
//HTML = e(">",">");
buf=HTML;
return true;
}
catch (Exception x)
{
buf=ng();
return false;
}
}
带Cookie:
CookieContainer cc = new CookieContainer();
public bool getweb(string strURL,out string buf)
{
buf="";
try
{
HttpWebRequest request;
request = (HttpWebRequest)(strURL);
="POST"; //Post请求方式
tType="text/html;charset=gb2312"; //内容类型
string paraUrlCoded = ode(""); //参数经过URL编码
byte[] payload;
payload = oding("GB2312").GetBytes(paraUrlCoded); //将URL编码后的字符串转化为字节
tLength = ; //设置请求的ContentLength
Stream writer = uestStream(); //获得请求流
(payload,0,); //将请求参数写入流
(); //关闭请求流
HttpWebResponse response;
response = (HttpWebResponse)ponse(); //获得响应流
Stream s;
s = ponseStream();
StreamReader objReader = new StreamReader(s,oding("GB2312"));
string HTML = "";
string sLine ="";
int i = 0;
while (sLine!=null)
{
i++;
sLine = ne();
if (sLine!=null)
HTML += sLine;
}
buf=HTML;
return true;
}
catch (Exception x)
{
buf=ng();
return false;
}
}
public bool getweb(string strURL,out string buf,string postData)
{
buf="";
try
{
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = es(postData);
HttpWebRequest request = (HttpWebRequest)(strURL);
= "POST";
tType = "application/x-www-form-urlencoded";
tLength = ;
Stream newStream = uestStream();
(data, 0, );
();
Container = cc;
HttpWebResponse response = (HttpWebResponse)ponse();
(s);
Stream stream = ponseStream();
string sHtml = new StreamReader(stream, t).ReadToEnd();
buf=sHtml;
return true;
}
catch (Exception x)
{
buf=ng();
return false;
}
}
private string getWebresourceFile1(string url)
{
WebClient myWebClient = new WebClient();
byte[] myDataBuffer = adData(url);
string SourceCode = ing(myDataBuffer);
saveSourceCode(SourceCode);
return SourceCode;
}
方法2
private string getWebresourceFile2(string url)
{
HttpWebRequest request=(HttpWebRequest)(ur
l);
HttpWebResponse response=(HttpWebResponse)ponse();
= "GET ";
Stream receiveStream=ponseStream();
StreamReader readStream=new StreamReader(receiveStream,t);
string SourceCode=End();
saveSourceCode(SourceCode);
();
();
return SourceCode;
}
方法3
private string getWebresourceFile3(string url)
{
WebClient wc = new WebClient();
tials = tCredentials;
Byte[] pageData = adData(url);
string SourceCode = ing(pageData);
saveSourceCode(SourceCode);
e();
return SourceCode;
}
方法4
private string getWebresourceFile4(string url)
{
WebClient wc = new WebClient();
tials = tCredentials;
Stream resStream = ad(url);
StreamReader sr = new StreamReader(resStream,t);
string SourceCode = End();
saveSourceCode(SourceCode);
();
e();
return SourceCode;
}
方法5
private string getWebresourceFile5(string url)
{
WebRequest request = (url);
WebResponse response = ponse();
Stream resStream = ponseStream();
StreamReader sr = new StreamReader(resStream, t);
string SourceCode = End();
saveSourceCode(SourceCode);
();
();
return SourceCode;
}
版权声明:本文标题:C#获取网页源代码的5种方法 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/free/1703231376h443238.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论