1、HttpWebRequest登录方法:
public static string HttpPost(string Url, string PostData, string cookies,string UA,out string result)
{
result = null;
HttpWebRequest request = null;
HttpWebResponse response = null;
try
{
request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST";
byte[] data = Encoding.UTF8.GetBytes(PostData);
request.ContentType = "application/x-www-form-urlencoded";
request.ServicePoint.ConnectionLimit = 3000;
request.UserAgent = UA;
request.ContentLength = data.Length;
request.Headers.Add("cookie", cookies);
Stream newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
result = reader.ReadToEnd();
reader.Close();
request.Abort();
response.Close();
return result;
}
catch (Exception ex)
{
if (request != null) request.Abort();
if (response != null) response.Close();
MessageBox.Show("报错,原因:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return string.Empty;
}
}
在需要的地方用如下代码调用即可:
HttpPost(Url, PostData, cookies, UA, out result);
文章评论 本文章有个评论