/// <summary> /// 构造新的HttpClient实例 /// </summary> /// <param name="url">要获取的资源的地址</param> /// <param name="context">Cookie及Referer</param> /// <param name="keepContext">是否自动在不同的请求间保留Cookie, Referer</param> public HttpClient(string url, HttpClientContext context, bool keepContext) { this.url = url; this.context = context; this.keepContext = keepContext; this.Language = CultureInfo.CreateSpecificCulture("zh-CN");//EN-US if (this.context == null) { this.context = new HttpClientContext(); } // 默认重试次数为2 this.RetryTime = 2; }
/// <summary> /// 清空PostingData, Files, StartPoint, EndPoint, headers, 并把Verb设置为Get. /// 在发出一个包含上述信息的请求后,必须调用此方法或手工设置相应属性以使下一次请求不会受到影响. /// </summary> public void ClearHttpClientState() { if (MemoryStream != null) { MemoryStream.Close(); MemoryStream = null; } if (this.webRequest != null) { this.webRequest.Abort(); this.webRequest = null; } if (this.webResponse != null) { this.webResponse.Close(); this.webResponse = null; } if (this.files != null) { this.files.Clear(); } if (this.postData != null) { this.postData.Clear(); } this.context = null; this.headers = null; this.httpMethod = HttpMethod.Get; this.contentLength = -1; this.startPoint = 0; this.endPoint = 0; }
/// <summary> /// 构造新的HttpClient实例 /// </summary> /// <param name="url">要获取的资源的地址</param> /// <param name="context">Cookie及Referer</param> public HttpClient(string url, HttpClientContext context) : this(url, context, false) { }