码迷,mamicode.com
首页 > Web开发 > 详细

WebClient Post 数据 ,支持Cookie

时间:2014-06-08 10:50:57      阅读:324      评论:0      收藏:0      [点我收藏+]

标签:c   class   blog   code   a   http   

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,布布扣,bubuko.com

WebClient Post 数据 ,支持Cookie

标签:c   class   blog   code   a   http   

原文地址:http://blog.csdn.net/luofeng0710/article/details/28895463

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!