如有不明白的地方欢迎加QQ群
14670545 探讨
///<summary>
/// 获取指定url的请求内容
///</summary>
///<param name="Url"></param>
///<param name="encode"></param>
///<returns></returns>
public static string GetRemoteHtmlCodeByEncoding(string Url, string encode)
{
HttpWebResponse Result = null;
try
{
HttpWebRequest Req = (HttpWebRequest) System.Net.WebRequest.Create(Url);
Req.Method = "get";
Req.ContentType = "application/x-www-form-urlencoded";
Req.Credentials = CredentialCache.DefaultCredentials;
Result = (HttpWebResponse) Req.GetResponse();
StreamReader ReceiveStream = new StreamReader(Result.GetResponseStream(), Encoding.GetEncoding(encode));
string OutPutString;
try
{
OutPutString = ReceiveStream.ReadToEnd();
}
catch
{
OutPutString = string.Empty;
}
finally
{
ReceiveStream.Close();
}
return OutPutString;
}
catch
{
return string.Empty;
}
finally
{
if (Result != null)
{
Result.Close();
}
}
}
/// <summary>
/// 以POST方式抓取远程页面内容
/// </summary>
/// <param name="url">请求的url</param>
/// <param name="postData">参数列表</param>
/// <param name="encodeType">编码类型</param>
/// <returns></returns>
public static string Post_Http(string url, string postData, string encodeType)
{
HttpWebResponse myResponse = null;
string strResult = null;
try
{
Encoding encoding = Encoding.GetEncoding(encodeType);
byte[] POST = encoding.GetBytes(postData);
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = POST.Length;
Stream newStream = myRequest.GetRequestStream();
newStream.Write(POST, 0, POST.Length); //设置POST
newStream.Close();
// 获取结果数据
myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);
strResult = reader.ReadToEnd();
}
catch (Exception ex)
{
strResult = ex.Message;
}
finally
{
if (myResponse != null)
myResponse.Close();
}
return strResult;
}