WebClient web = new CookieWebClient(); web.Encoding = Encoding.UTF8; string regUrl = "http://test.com"; web.Headers.Add("Content-Type","POST", "application/x-www-form-urlencoded"); byte[] post = Encoding.UTF8.GetBytes("u=aa&p=bb");byte[] data = web.UploadData(regUrl, post);string html = Encoding.UTF8.GetString(data);
/// <summary> /// 带 Cookie 的 WebClient /// </summary> public class CookieWebClient : WebClient { // Cookie 容器 public CookieContainer Cookies; /// <summary> /// 创建一个新的 CookieWebClient 实例。 /// </summary> public CookieWebClient() { this.Cookies = new CookieContainer(); } protected override WebRequest GetWebRequest(Uri address) { WebRequest request = base.GetWebRequest(address); if (request is HttpWebRequest) { HttpWebRequest httpRequest = request as HttpWebRequest; httpRequest.CookieContainer = Cookies; } return request; } }
WebClient Post 数据 ,支持Cookie
时间: 2024-10-12 17:30:33