首页 > Web开发 > 详细

WebClient Post 数据 ,支持Cookie

时间:2014-06-08 10:51:04      阅读:425      评论:0      收藏:0      [点我收藏+]
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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!