/// <summary>
        /// set proxy
        /// </summary>
        /// <param name="httpWebRequest"></param>
        /// <param name="config"></param>
        private static void SetRequestProxy(HttpWebRequest httpWebRequest, HttpClientConfig config)
        {
            httpWebRequest.Proxy = null;

            if (!String.IsNullOrEmpty(config.ProxyHost))
            {
                if (config.ProxyPort < 0)
                {
                    httpWebRequest.Proxy = new WebProxy(config.ProxyHost);
                }
                else
                {
                    httpWebRequest.Proxy = new WebProxy(config.ProxyHost, config.ProxyPort);
                }

                if (!String.IsNullOrEmpty(config.ProxyUserName))
                {
                    httpWebRequest.Proxy.Credentials = String.IsNullOrEmpty(config.ProxyDomain) ?
                                                       new NetworkCredential(config.ProxyUserName, config.ProxyUserPassword ?? String.Empty) :
                                                       new NetworkCredential(config.ProxyUserName, config.ProxyUserPassword ?? String.Empty,
                                                                             config.ProxyDomain);
                }
                // 代理验证
                // 代理验证
                httpWebRequest.PreAuthenticate = true;
            }
        }
Esempio n. 2
0
 public static void Init(HttpClientConfig config, QCloudCredentialProvider credentialsProvider)
 {
     lock (sync)
     {
         HttpClient.config = config;
         HttpClient.credentialsProvider = credentialsProvider;
         // init grobal httpwebreqeust
         CommandTask.Init(HttpClient.config);
     }
 }
Esempio n. 3
0
        /// <summary>
        /// async to excute
        /// </summary>
        /// <param name="request"></param>
        /// <param name="response"></param>
        /// <param name="config"></param>
        public static void Schedue(Request request, Response response, HttpClientConfig config)
        {
            HttpWebRequest httpWebRequest = null;
            RequestState   requestState   = new RequestState();

            try
            {
                requestState.request = request;

                requestState.response = response;

                httpWebRequest = WebRequest.Create(request.RequestUrlString) as HttpWebRequest;

                httpWebRequest.AllowWriteStreamBuffering = false;

                //bind webRequest
                request.BindHttpWebRequest(httpWebRequest);

                //handle request header
                HandleHttpWebRequestHeaders(request, httpWebRequest, config);

                requestState.httpWebRequest = httpWebRequest;

                //handle request body
                if (request.Body != null)
                {
                    httpWebRequest.ContentLength = request.Body.ContentLength;
                    httpWebRequest.BeginGetRequestStream(new AsyncCallback(AsyncRequestCallback), requestState);
                }
                else
                {
                    //wait for response
                    httpWebRequest.BeginGetResponse(new AsyncCallback(AsyncResponseCallback), requestState);
                }

                //print log
                PrintReqeustInfo(httpWebRequest);
            }
            catch (WebException webEx)
            {
                response.OnFinish(false, webEx);
                //abort
                requestState.Clear();
                QLog.D(TAG, webEx.Message, webEx);
            }
            catch (Exception ex)
            {
                response.OnFinish(false, ex);
                //abort
                requestState.Clear();
                QLog.E(TAG, ex.Message, ex);
            }
        }
Esempio n. 4
0
        public HttpClient(HttpClientConfig config)
        {
            if (config != null)
            {
                this.config = config;
            }
            else
            {
                this.config = new HttpClientConfig.Builder().Build();
            }

            // init grobal httpwebreqeust
            CommandTask.Init(this.config);
        }
Esempio n. 5
0
 public static void Init(HttpClientConfig config, QCloudCredentialProvider credentialsProvider)
 {
     lock (sync)
     {
         if (config == null)
         {
             throw new CosClientException((int)CosClientError.INVALID_ARGUMENT, "HttpClientConfig = null");
         }
         HttpClient.config = config;
         HttpClient.credentialsProvider = credentialsProvider;
         // init grobal httpwebreqeust
         CommandTask.Init(HttpClient.config);
     }
 }
Esempio n. 6
0
 public void Init(HttpClientConfig config)
 {
     if (this.config == null)
     {
         lock (sync)
         {
             if (this.config == null)
             {
                 this.config = config;
                 // init grobal httpwebreqeust
                 CommandTask.Init(this.config);
             }
         }
     }
 }
Esempio n. 7
0
        /// <summary>
        /// sync excute
        /// </summary>
        /// <param name="request"></param>
        /// <param name="config"></param>
        /// <returns></returns>
        public static void excute(Request request, Response response, HttpClientConfig config)
        {
            //step1: create HttpWebRequest by request.url
            HttpWebRequest httpWebRequest = HttpWebRequest.Create(request.Url.ToString()) as HttpWebRequest;

            // handler request
            HandleHttpWebRequest(httpWebRequest, request, config);

            //final: get response
            HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;

            //print log
            printReqeustInfo(httpWebRequest);

            //handle response
            HandleHttpWebResponse(httpWebResponse, response);
        }
Esempio n. 8
0
        /// <summary>
        /// sync excute
        /// </summary>
        /// <param name="request"></param>
        /// <param name="config"></param>
        /// <returns></returns>
        public static void Excute(Request request, Response response, HttpClientConfig config)
        {
            HttpWebRequest  httpWebRequest  = null;
            HttpWebResponse httpWebResponse = null;

            try
            {
                //step1: create HttpWebRequest by request.url
                httpWebRequest = HttpWebRequest.Create(request.RequestUrlString) as HttpWebRequest;

                httpWebRequest.AllowWriteStreamBuffering = false;

                //bind webRequest
                request.BindHttpWebRequest(httpWebRequest);

                // handler request
                HandleHttpWebRequest(httpWebRequest, request, config);

                //final: get response
                httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;

                //notify has been got response
                request.onNotifyGetResponse();

                //handle response for [100, 300)
                HandleHttpWebResponse(httpWebResponse, response);
            }
            catch (WebException webEx)
            {
                if (webEx.Response != null && webEx.Response is HttpWebResponse)
                {
                    //notify has been got response

                    request.onNotifyGetResponse();

                    httpWebResponse = (HttpWebResponse)webEx.Response;
                    //handle response for [400, 500]
                    HandleHttpWebResponse(httpWebResponse, response);
                }
                else
                {
                    //QLog.E(TAG, webEx.Message, webEx);
                    throw;
                }
            }
            catch (Exception ex)
            {
                //QLog.E(TAG, ex.Message, ex);
                throw;
            }
            finally
            {
                if (httpWebResponse != null)
                {
                    // print log
                    PrintResponseInfo(httpWebResponse);
                    httpWebResponse.Close();
                    //QLog.D("XIAO", "response close");
                }
                if (httpWebRequest != null)
                {
                    httpWebRequest.Abort();
                    //QLog.D("XIAO", "request close");
                }
                QLog.D(TAG, "close");
            }
        }
Esempio n. 9
0
 /// <summary>
 /// init connectionLimit and statueCode = 100 action
 /// </summary>
 /// <param name="config"></param>
 public static void Init(HttpClientConfig config)
 {
     ServicePointManager.Expect100Continue      = false;
     ServicePointManager.DefaultConnectionLimit = config.ConnectionLimit;
 }
Esempio n. 10
0
        /// <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;
        }
Esempio n. 11
0
 /// <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)
 {
     HandleHttpWebRequestHeaders(request, httpWebRequest, config);
     //setp5: send request content: body
     if (request.Body != null)
     {
         httpWebRequest.ContentLength = request.Body.ContentLength;
         request.Body.OnWrite(httpWebRequest.GetRequestStream());
     }
     //print request start log
     PrintReqeustInfo(httpWebRequest);
 }
Esempio n. 12
0
        /// <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());
            }
        }