示例#1
0
        /// <summary>
        /// Return an HttpWebResponse object for a request. You can use the Response to
        /// read the result as needed. This is a low level method. Most of the other 'Get'
        /// methods call this method and process the results further.
        /// </summary>
        /// <remarks>Important: The Response object's Close() method must be called when you are done with the object.</remarks>
        /// <param name="url">Url to retrieve.</param>
        /// <returns>An HttpWebResponse Object</returns>
        public HttpWebResponse  DownloadResponse(string url)
        {
            Cancelled = false;

            //try
            //{
            _Error        = false;
            _ErrorMessage = string.Empty;
            _Cancelled    = false;

            if (WebRequest == null)
            {
                WebRequest = (HttpWebRequest)System.Net.WebRequest.Create(url);
                WebRequest.Headers.Add("Cache", "no-cache");
            }


            WebRequest.UserAgent        = _UserAgent;
            WebRequest.Timeout          = _timeout * 1000;
            WebRequest.ReadWriteTimeout = _timeout * 1000;

            // Handle Security for the request
            if (_Username.Length > 0)
            {
                if (_Username == "AUTOLOGIN" || _Username == "AutoLogin")
                {
                    WebRequest.Credentials = CredentialCache.DefaultCredentials;
                }
                else
                {
                    WebRequest.Credentials = new NetworkCredential(_Username, _Password);
                }
            }

            // Handle Proxy Server configuration
            if (_ProxyAddress.Length > 0)
            {
                if (_ProxyAddress == "DEFAULTPROXY")
                {
                    WebRequest.Proxy = HttpWebRequest.DefaultWebProxy;
                }
                else
                {
                    WebProxy Proxy = new WebProxy(_ProxyAddress, true);

                    if (_ProxyBypass.Length > 0)
                    {
                        Proxy.BypassList = _ProxyBypass.Split(';');
                    }

                    if (_ProxyUsername.Length > 0)
                    {
                        Proxy.Credentials = new NetworkCredential(_ProxyUsername, _ProxyPassword);
                    }

                    WebRequest.Proxy = Proxy;
                }
            }

            if (UseGZip)
            {
                WebRequest.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
            }

            // Handle cookies - automatically re-assign
            if (_HandleCookies || (_Cookies != null && _Cookies.Count > 0))
            {
                WebRequest.CookieContainer = new CookieContainer();
                if (_Cookies != null && _Cookies.Count > 0)
                {
                    WebRequest.CookieContainer.Add(_Cookies);
                }
            }

            HttpTimings.StartRequest();

            // Deal with the POST buffer if any
            if (_PostData != null)
            {
                if (WebRequest.Method == "GET")
                {
                    WebRequest.Method = "POST";
                }

                switch (_PostMode)
                {
                case HttpPostMode.UrlEncoded:
                    WebRequest.ContentType = "application/x-www-form-urlencoded";
                    break;

                case HttpPostMode.MultiPart:
                    WebRequest.ContentType = "multipart/form-data; boundary=" + _MultiPartBoundary;
                    _PostData.Write(Encoding.GetEncoding(1252).GetBytes("--" + _MultiPartBoundary + "--\r\n"));
                    break;

                case HttpPostMode.Xml:
                    WebRequest.ContentType = "text/xml";
                    break;

                case HttpPostMode.Json:
                    WebRequest.ContentType = "application/json";
                    break;

                case HttpPostMode.Raw:
                    //WebRequest.ContentType = "application/octet-stream";
                    break;

                default:
                    goto case HttpPostMode.UrlEncoded;
                }

                if (!string.IsNullOrEmpty(ContentType))
                {
                    WebRequest.ContentType = ContentType;
                }

                Stream requestStream = WebRequest.GetRequestStream();


                if (SendData == null)
                {
                    _PostStream.WriteTo(requestStream);                              // Simplest version - no events
                }
                else
                {
                    StreamPostBuffer(requestStream);                                 // Send in chunks and fire events
                }
                //*** Close the memory stream
                _PostStream.Close();
                _PostStream = null;

                //*** Close the Binary Writer
                if (_PostData != null)
                {
                    _PostData.Close();
                    _PostData = null;
                }
                //*** Close Request Stream
                requestStream.Close();

                // clear out the Post buffer
                ResetPostData();

                // If user cancelled the 'upload' exit
                if (Cancelled)
                {
                    ErrorMessage = "HTTP Request was cancelled.";
                    Error        = true;
                    return(null);
                }
            }

            // Retrieve the response headers
            HttpWebResponse Response = null;

            try
            {
                Response = (HttpWebResponse)WebRequest.GetResponse();
            }
            catch (WebException ex)
            {
                // Check for 500 error return - if so we still want to return a response
                // Client can check oHttp.WebResponse.StatusCode
                if (ex.Status == WebExceptionStatus.ProtocolError)
                {
                    Response = (HttpWebResponse)ex.Response;
                }
                else
                {
                    throw;
                }
            }

            _WebResponse = Response;

            // Close out the request - it cannot be reused
            WebRequest = null;

            // ** Save cookies the server sends
            if (_HandleCookies)
            {
                if (Response.Cookies.Count > 0)
                {
                    if (_Cookies == null)
                    {
                        _Cookies = Response.Cookies;
                    }
                    else
                    {
                        // ** If we already have cookies update the list
                        foreach (Cookie oRespCookie in Response.Cookies)
                        {
                            bool bMatch = false;
                            foreach (Cookie oReqCookie in _Cookies)
                            {
                                if (oReqCookie.Name == oRespCookie.Name)
                                {
                                    oReqCookie.Value = oRespCookie.Value;
                                    bMatch           = true;
                                    break;            //
                                }
                            }                         // for each ReqCookies
                            if (!bMatch)
                            {
                                _Cookies.Add(oRespCookie);
                            }
                        }
                    }
                }
            }

            return(Response);
        }
示例#2
0
		/// <summary>
		/// The HttpClient Default Constructor
		/// </summary>
		public HttpClient()
		{
		    HttpTimings = new HttpTimings();
		}
示例#3
0
 /// <summary>
 /// The HttpClient Default Constructor
 /// </summary>
 public HttpClient()
 {
     HttpTimings = new HttpTimings();
 }