C#几种获取网页源文件代码的实例
程序员文章站
2023-12-17 14:27:34
方法一:复制代码 代码如下:using system.text;using system.net;private string gethtml(string url){we...
方法一:
using system.text;
using system.net;
private string gethtml(string url)
{
webclient mywebclient = new webclient();
byte[] mydatabuffer = mywebclient.downloaddata (url);
return encoding.default.getstring(mydatabuffer);
}
方法二:
public string gethttp(string httpurl,string refererurl)
{
string html = "";
try
{
msxml2.xmlhttp http = new msxml2.xmlhttpclass();
xmlhttp.open("get", url, false, null, null);
http.open("get",httpurl,false,null,null);
http.setrequestheader("referer",refererurl);
//http.setrequestheader("referer",refererurl);
http.setrequestheader("content-type", "text/html;charset=gb2312");
http.send("");
html = encoding.default.getstring((byte[])http.responsebody);
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)webrequest.create(strurl);
request.method="post"; //post请求方式
request.contenttype="text/html;charset=gb2312"; //内容类型
string paraurlcoded = system.web.httputility.urlencode(""); //参数经过url编码
byte[] payload;
payload = system.text.encoding.getencoding("gb2312").getbytes(paraurlcoded); //将url编码后的字符串转化为字节
request.contentlength = payload.length; //设置请求的contentlength
stream writer = request.getrequeststream(); //获得请求流
writer.write(payload,0,payload.length); //将请求参数写入流
writer.close(); //关闭请求流
httpwebresponse response;
response = (httpwebresponse)request.getresponse(); //获得响应流
stream s;
s = response.getresponsestream();
streamreader objreader = new streamreader(s,system.text.encoding.getencoding("gb2312"));
string html = "";
string sline ="";
int i = 0;
while (sline!=null)
{
i++;
sline = objreader.readline();
if (sline!=null)
html += sline;
}
//html = html.replace("<","<");
//html = html.replace(">",">");
buf=html;
return true;
}
catch (exception x)
{
buf=x.message.tostring();
return false;
}
}
带cookie:
cookiecontainer cc = new cookiecontainer();
public bool getweb(string strurl,out string buf)
{
buf="";
try
{
httpwebrequest request;
request = (httpwebrequest)webrequest.create(strurl);
request.method="post"; //post请求方式
request.contenttype="text/html;charset=gb2312"; //内容类型
string paraurlcoded = system.web.httputility.urlencode(""); //参数经过url编码
byte[] payload;
payload = system.text.encoding.getencoding("gb2312").getbytes(paraurlcoded); //将url编码后的字符串转化为字节
request.contentlength = payload.length; //设置请求的contentlength
stream writer = request.getrequeststream(); //获得请求流
writer.write(payload,0,payload.length); //将请求参数写入流
writer.close(); //关闭请求流
httpwebresponse response;
response = (httpwebresponse)request.getresponse(); //获得响应流
stream s;
s = response.getresponsestream();
streamreader objreader = new streamreader(s,system.text.encoding.getencoding("gb2312"));
string html = "";
string sline ="";
int i = 0;
while (sline!=null)
{
i++;
sline = objreader.readline();
if (sline!=null)
html += sline;
}
buf=html;
return true;
}
catch (exception x)
{
buf=x.message.tostring();
return false;
}
}
public bool getweb(string strurl,out string buf,string postdata)
{
buf="";
try
{
asciiencoding encoding = new asciiencoding();
byte[] data = encoding.getbytes(postdata);
httpwebrequest request = (httpwebrequest)webrequest.create(strurl);
request.method = "post";
request.contenttype = "application/x-www-form-urlencoded";
request.contentlength = data.length;
stream newstream = request.getrequeststream();
newstream.write(data, 0, data.length);
newstream.close();
request.cookiecontainer = cc;
httpwebresponse response = (httpwebresponse)request.getresponse();
cc.add(response.cookies);
stream stream = response.getresponsestream();
string shtml = new streamreader(stream, system.text.encoding.default).readtoend();
buf=shtml;
return true;
}
catch (exception x)
{
buf=x.message.tostring();
return false;
}
}
private string getwebresourcefile1(string url)
{
webclient mywebclient = new webclient();
byte[] mydatabuffer = mywebclient.downloaddata(url);
string sourcecode = encoding.default.getstring(mydatabuffer);
savesourcecode(sourcecode);
return sourcecode;
}
方法2
private string getwebresourcefile2(string url)
{
httpwebrequest request=(httpwebrequest)webrequest.create(url);
httpwebresponse response=(httpwebresponse)request.getresponse();
request.method = "get ";
stream receivestream=response.getresponsestream();
streamreader readstream=new streamreader(receivestream,encoding.default);
string sourcecode=readstream.readtoend();
savesourcecode(sourcecode);
response.close();
readstream.close();
return sourcecode;
}
方法3
private string getwebresourcefile3(string url)
{
webclient wc = new webclient();
wc.credentials = credentialcache.defaultcredentials;
byte[] pagedata = wc.downloaddata(url);
string sourcecode = encoding.default.getstring(pagedata);
savesourcecode(sourcecode);
wc.dispose();
return sourcecode;
}
方法4
private string getwebresourcefile4(string url)
{
webclient wc = new webclient();
wc.credentials = credentialcache.defaultcredentials;
stream resstream = wc.openread(url);
streamreader sr = new streamreader(resstream,system.text.encoding.default);
string sourcecode = sr.readtoend();
savesourcecode(sourcecode);
resstream.close();
wc.dispose();
return sourcecode;
}
方法5
private string getwebresourcefile5(string url)
{
webrequest request = webrequest.create(url);
webresponse response = request.getresponse();
stream resstream = response.getresponsestream();
streamreader sr = new streamreader(resstream, system.text.encoding.default);
string sourcecode = sr.readtoend();
savesourcecode(sourcecode);
resstream.close();
sr.close();
return sourcecode;
}
复制代码 代码如下:
using system.text;
using system.net;
private string gethtml(string url)
{
webclient mywebclient = new webclient();
byte[] mydatabuffer = mywebclient.downloaddata (url);
return encoding.default.getstring(mydatabuffer);
}
方法二:
复制代码 代码如下:
public string gethttp(string httpurl,string refererurl)
{
string html = "";
try
{
msxml2.xmlhttp http = new msxml2.xmlhttpclass();
xmlhttp.open("get", url, false, null, null);
http.open("get",httpurl,false,null,null);
http.setrequestheader("referer",refererurl);
//http.setrequestheader("referer",refererurl);
http.setrequestheader("content-type", "text/html;charset=gb2312");
http.send("");
html = encoding.default.getstring((byte[])http.responsebody);
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)webrequest.create(strurl);
request.method="post"; //post请求方式
request.contenttype="text/html;charset=gb2312"; //内容类型
string paraurlcoded = system.web.httputility.urlencode(""); //参数经过url编码
byte[] payload;
payload = system.text.encoding.getencoding("gb2312").getbytes(paraurlcoded); //将url编码后的字符串转化为字节
request.contentlength = payload.length; //设置请求的contentlength
stream writer = request.getrequeststream(); //获得请求流
writer.write(payload,0,payload.length); //将请求参数写入流
writer.close(); //关闭请求流
httpwebresponse response;
response = (httpwebresponse)request.getresponse(); //获得响应流
stream s;
s = response.getresponsestream();
streamreader objreader = new streamreader(s,system.text.encoding.getencoding("gb2312"));
string html = "";
string sline ="";
int i = 0;
while (sline!=null)
{
i++;
sline = objreader.readline();
if (sline!=null)
html += sline;
}
//html = html.replace("<","<");
//html = html.replace(">",">");
buf=html;
return true;
}
catch (exception x)
{
buf=x.message.tostring();
return false;
}
}
带cookie:
复制代码 代码如下:
cookiecontainer cc = new cookiecontainer();
public bool getweb(string strurl,out string buf)
{
buf="";
try
{
httpwebrequest request;
request = (httpwebrequest)webrequest.create(strurl);
request.method="post"; //post请求方式
request.contenttype="text/html;charset=gb2312"; //内容类型
string paraurlcoded = system.web.httputility.urlencode(""); //参数经过url编码
byte[] payload;
payload = system.text.encoding.getencoding("gb2312").getbytes(paraurlcoded); //将url编码后的字符串转化为字节
request.contentlength = payload.length; //设置请求的contentlength
stream writer = request.getrequeststream(); //获得请求流
writer.write(payload,0,payload.length); //将请求参数写入流
writer.close(); //关闭请求流
httpwebresponse response;
response = (httpwebresponse)request.getresponse(); //获得响应流
stream s;
s = response.getresponsestream();
streamreader objreader = new streamreader(s,system.text.encoding.getencoding("gb2312"));
string html = "";
string sline ="";
int i = 0;
while (sline!=null)
{
i++;
sline = objreader.readline();
if (sline!=null)
html += sline;
}
buf=html;
return true;
}
catch (exception x)
{
buf=x.message.tostring();
return false;
}
}
public bool getweb(string strurl,out string buf,string postdata)
{
buf="";
try
{
asciiencoding encoding = new asciiencoding();
byte[] data = encoding.getbytes(postdata);
httpwebrequest request = (httpwebrequest)webrequest.create(strurl);
request.method = "post";
request.contenttype = "application/x-www-form-urlencoded";
request.contentlength = data.length;
stream newstream = request.getrequeststream();
newstream.write(data, 0, data.length);
newstream.close();
request.cookiecontainer = cc;
httpwebresponse response = (httpwebresponse)request.getresponse();
cc.add(response.cookies);
stream stream = response.getresponsestream();
string shtml = new streamreader(stream, system.text.encoding.default).readtoend();
buf=shtml;
return true;
}
catch (exception x)
{
buf=x.message.tostring();
return false;
}
}
private string getwebresourcefile1(string url)
{
webclient mywebclient = new webclient();
byte[] mydatabuffer = mywebclient.downloaddata(url);
string sourcecode = encoding.default.getstring(mydatabuffer);
savesourcecode(sourcecode);
return sourcecode;
}
方法2
复制代码 代码如下:
private string getwebresourcefile2(string url)
{
httpwebrequest request=(httpwebrequest)webrequest.create(url);
httpwebresponse response=(httpwebresponse)request.getresponse();
request.method = "get ";
stream receivestream=response.getresponsestream();
streamreader readstream=new streamreader(receivestream,encoding.default);
string sourcecode=readstream.readtoend();
savesourcecode(sourcecode);
response.close();
readstream.close();
return sourcecode;
}
方法3
复制代码 代码如下:
private string getwebresourcefile3(string url)
{
webclient wc = new webclient();
wc.credentials = credentialcache.defaultcredentials;
byte[] pagedata = wc.downloaddata(url);
string sourcecode = encoding.default.getstring(pagedata);
savesourcecode(sourcecode);
wc.dispose();
return sourcecode;
}
方法4
复制代码 代码如下:
private string getwebresourcefile4(string url)
{
webclient wc = new webclient();
wc.credentials = credentialcache.defaultcredentials;
stream resstream = wc.openread(url);
streamreader sr = new streamreader(resstream,system.text.encoding.default);
string sourcecode = sr.readtoend();
savesourcecode(sourcecode);
resstream.close();
wc.dispose();
return sourcecode;
}
方法5
复制代码 代码如下:
private string getwebresourcefile5(string url)
{
webrequest request = webrequest.create(url);
webresponse response = request.getresponse();
stream resstream = response.getresponsestream();
streamreader sr = new streamreader(resstream, system.text.encoding.default);
string sourcecode = sr.readtoend();
savesourcecode(sourcecode);
resstream.close();
sr.close();
return sourcecode;
}