Nó khá đơn giản. bạn cần phương pháp của bạn tùy chỉnh đăng nhập (HttpPost).
bạn có thể đưa ra một cái gì đó như thế này (theo cách này bạn sẽ nhận được tất cả các cookie cần thiết sau khi đăng nhập và bạn chỉ cần chuyển chúng đến HttpWebRequest tiếp theo):
public static HttpWebResponse HttpPost(String url, String referer, String userAgent, ref CookieCollection cookies, String postData, out WebHeaderCollection headers, WebProxy proxy)
{
try
{
HttpWebRequest http = WebRequest.Create(url) as HttpWebRequest;
http.Proxy = proxy;
http.AllowAutoRedirect = true;
http.Method = "POST";
http.ContentType = "application/x-www-form-urlencoded";
http.UserAgent = userAgent;
http.CookieContainer = new CookieContainer();
http.CookieContainer.Add(cookies);
http.Referer = referer;
byte[] dataBytes = UTF8Encoding.UTF8.GetBytes(postData);
http.ContentLength = dataBytes.Length;
using (Stream postStream = http.GetRequestStream())
{
postStream.Write(dataBytes, 0, dataBytes.Length);
}
HttpWebResponse httpResponse = http.GetResponse() as HttpWebResponse;
headers = http.Headers;
cookies.Add(httpResponse.Cookies);
return httpResponse;
}
catch { }
headers = null;
return null;
}
Nguồn
2009-05-23 08:46:54
Mẹo: Ngay cả khi bạn không muốn phản hồi từ POST, điều quan trọng là phải ăn nó để chuyển dữ liệu bị xóa và kết nối đóng sạch, ví dụ: {http.GetResponse(); } – kgriffs