代码
引入命名空间
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
//以GET方式抓取远程页面内容
public string Get_Http(string tUrl)
{
string strResult;
try
{
HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(tUrl);
hwr.Timeout = 19600;
HttpWebResponse hwrs = (HttpWebResponse)hwr.GetResponse();
Stream myStream = hwrs.GetResponseStream();
StreamReader sr = new StreamReader(myStream, Encoding.Default);
StringBuilder sb = new StringBuilder();
while (-1 != sr.Peek())
{
sb.Append(sr.ReadLine() + "\r\n");
}
strResult = sb.ToString();
hwrs.Close();
}
catch (Exception ee)
{
strResult = ee.Message;
}
return strResult;
}
//以POST方式抓取远程页面内容
//postData为参数列表
public string Post_Http(string url, string postData, string encodeType, out string err)
{
string uriString = url;
byte[] byteArray;
byte[] responseArray;
//postData = "checkvalue=32&bbb=%CD%B6%C6%B1&ilc=0&kkk=22";
Encoding encoding = Encoding.GetEncoding(encodeType);
try
{
WebClient myWebClient = new WebClient();
WebHeaderCollection myWebHeaderCollection;
myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
//myWebClient.Headers.Add("Referer","http://xxxxx/xxxxxxxxxxxxxxxxxxxxxxxx");
//myWebClient.Headers.Add("Accept-Language","zh-cn");
myWebHeaderCollection = myWebClient.Headers;
byteArray = encoding.GetBytes(postData);
responseArray = myWebClient.UploadData(uriString, "POST", byteArray);
err = string.Empty;
return this.StrClearNoUse(encoding.GetString(responseArray));
}
catch (Exception ex)
{
err = ex.Message;
return string.Empty;
}
}