get post方法
#region 根据URL获取网页内容
/// <summary>
/// 根据URL获取网页内容
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static String GetHtml_Get(string url, string cookies, Encoding encoding)//Encoding encoding=Encoding.Default
{
string strResult = "";
if (!String.IsNullOrEmpty(url))
{
try
{
ServicePointManager.DefaultConnectionLimit = 100;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.KeepAlive = false;
request.Timeout = 30000;
//设置连接超时时间
request.Headers.Set("Pragma", "no-cache");
if (!string.IsNullOrEmpty(cookies))
{
request.Headers.Add("cookie:" + cookies.Trim());
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream streamReceive = response.GetResponseStream();
//Encoding encoding = Encoding.GetEncoding("GB2312");
StreamReader streamReader = new StreamReader(streamReceive, encoding);
strResult = streamReader.ReadToEnd();
response.Close();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
return strResult;
}
public static string GetHtml_POST(string url, string cookies, string requestParam, Encoding encoding)
{
string Result = "";
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
//string s = "query.keyWord=" + System.Web.HttpUtility.UrlEncode("手机") + "&query.adType=0";//要传的参数
byte[] requestBytes = System.Text.Encoding.ASCII.GetBytes(requestParam);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.Headers.Add("cookie:" + cookies.Trim());
req.ContentLength = requestBytes.Length;
Stream requestStream = req.GetRequestStream();
requestStream.Write(requestBytes, 0, requestBytes.Length);
requestStream.Close();
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream(), encoding);
String line = sr.ReadToEnd();
sr.Close();
res.Close();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
return Result;
}
#endregion