时间:2021-05-20
方法一:
复制代码 代码如下:
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;
}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
详解C#编程获取资源文件中图片的方法本文主要介绍C#编程获取资源文件中图片的方法,涉及C#针对项目中资源文件操作的相关技巧,以供借鉴参考。具体内容如下:例子:u
本文实例为大家分享了C#操作INI配置文件示例的具体代码,供大家参考,具体内容如下源文件地址:C#操作INI配置文件示例创建如图所示的控件:源代码:usingS
本文实例讲述了C#编程获取资源文件中图片的方法。分享给大家供大家参考。具体实现方法如下:usingSystem;usingSystem.Collections.
本文实例讲述了C#获取网页HTML源码的方法,分享给大家供大家参考。具体方法如下:关键代码如下:复制代码代码如下://////获取网页HTML源码//////链
本文实例讲述了C#获取网页源代码的方法。分享给大家供大家参考。具体如下:publicstringGetPageHTML(stringurl){try{HttpW