/// <summary> /// handle request headers /// </summary> /// <param name="request"></param> /// <param name="httpWebRequest"></param> /// <param name="config"></param> private static void HandleHttpWebRequestHeaders(Request request, HttpWebRequest httpWebRequest, HttpClientConfig config) { // set connect timeout httpWebRequest.Timeout = config.ConnectionTimeoutMs; //set read write timeout httpWebRequest.ReadWriteTimeout = config.ReadWriteTimeoutMs; // set request method httpWebRequest.Method = request.Method.ToUpperInvariant(); // set user-agent httpWebRequest.UserAgent = request.UserAgent; //set host, net2.0 cannot set; // set allow auto redirect httpWebRequest.AllowAutoRedirect = config.AllowAutoRedirect; // notice: it is not allowed to set common headers with the WebHeaderCollection.Accept // such as: Connection,Content-Length,Content-Type,Date,Expect. Host,If-Modified-Since,Range, Referer,Transfer-Encoding,User-Agent,Proxy-Connection //step2: set header and connection properity by request.heders foreach (KeyValuePair <string, string> pair in request.Headers) { HttpHeaderHandle.AddHeader(httpWebRequest.Headers, pair.Key, pair.Value); } //step3: set proxy, default proxy = null, improte performation SetRequestProxy(httpWebRequest, config); //step4: https, default all true for "*.myqcloud.com" if (request.IsHttps) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationCertificate); } //初始化长度 httpWebRequest.ContentLength = 0L; }
/// <summary> /// handle request /// </summary> /// <param name="httpWebRequest"></param> /// <param name="request"></param> /// <param name="config"></param> private static void HandleHttpWebRequest(HttpWebRequest httpWebRequest, Request request, HttpClientConfig config) { // set connect timeout httpWebRequest.Timeout = config.ConnectionTimeoutMs; //set read write timeout httpWebRequest.ReadWriteTimeout = config.ReadWriteTimeoutMs; // set request method httpWebRequest.Method = request.Method.ToUpperInvariant(); // set user-agent httpWebRequest.UserAgent = config.UserAgnet; // set allow auto redirect httpWebRequest.AllowAutoRedirect = config.AllowAutoRedirect; // notice: it is not allowed to set common headers with the WebHeaderCollection.Accept // such as: Connection,Content-Length,Content-Type,Date,Expect. Host,If-Modified-Since,Range, Referer,Transfer-Encoding,User-Agent,Proxy-Connection //step2: set header and connection properity by request.heders for (int i = 0, count = request.Headers.Size(); i < count; i++) { HttpHeaderHandle.AddHeader(httpWebRequest.Headers, request.Headers.Name(i), request.Headers.Value(i)); } //step3: set proxy, default proxy = null, improte performation SetRequestProxy(httpWebRequest, config); //step4: https, default all true for "*.myqcloud.com" if (request.IsHttps) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationCertificate); } //setp5: send request content: body if (request.Body != null) { request.Body.OnWrite(httpWebRequest.GetRequestStream()); } }