WebClient Post 数据 ,支持Cookie
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; } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。