如何使WebClient支持Session
代码
public class HttpWebClient : WebClient
{
/// <summary>
/// CookieContainer
/// </summary>
private static CookieContainer _CookieContainer = new CookieContainer();
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
// 判断是不是HttpWebRequest.只有HttpWebRequest才有此属性
if (request is HttpWebRequest)
{
HttpWebRequest httpRequest = request as HttpWebRequest;
httpRequest.CookieContainer = _CookieContainer;
_CookieContainer.SetCookies(new Uri("http://127.0.0.1:1010/"), "ASP.NET_SessionId=zev02g55ynp5kp55xm3ihj45");
}
return request;
}
}