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;
	}
}


WebClient Post 数据 ,支持Cookie,布布扣,bubuko.com

WebClient Post 数据 ,支持Cookie

上一篇:我没发现Mvc里的 web.config 有什么用。


下一篇:迷你MVVM框架 avalonjs 沉思录 第3节 动态模板