示例#1
0
        public SocialHttpResponse GetResponse()
        {
            // Build the URL
            string url = Url;

            if (QueryString != null && !QueryString.IsEmpty)
            {
                url += (url.Contains("?") ? "&" : "?") + QueryString;
            }

            // Initialize the request
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            // Misc
            request.Method      = Method;
            request.Credentials = Credentials;
            request.Headers     = Headers.Headers;
            request.Accept      = Accept;
            request.Timeout     = (int)Timeout.TotalMilliseconds;

            // Add the request body (if a POST request)
            if (Method == "POST" && PostData != null && PostData.Count > 0)
            {
                string dataString = SocialUtils.NameValueCollectionToQueryString(PostData);
                request.ContentType   = "application/x-www-form-urlencoded";
                request.ContentLength = dataString.Length;
                using (Stream stream = request.GetRequestStream()) {
                    stream.Write(Encoding.UTF8.GetBytes(dataString), 0, dataString.Length);
                }
            }

            // Get the response
            try {
                return(SocialHttpResponse.GetFromWebResponse(request.GetResponse() as HttpWebResponse));
            } catch (WebException ex) {
                if (ex.Status != WebExceptionStatus.ProtocolError)
                {
                    throw;
                }
                return(SocialHttpResponse.GetFromWebResponse(ex.Response as HttpWebResponse));
            }
        }
        /// <summary>
        /// Executes the request and returns the corresponding response as an instance of <see cref="SocialHttpResponse"/>.
        /// </summary>
        /// <param name="callback">Lets you specify a callback method for modifying the underlying <see cref="HttpWebRequest"/>.</param>
        /// <returns>Returns an instance of <see cref="SocialHttpResponse"/> representing the response.</returns>
        public SocialHttpResponse GetResponse(Action <HttpWebRequest> callback)
        {
            // Build the URL
            string url = Url;

            if (QueryString != null && !QueryString.IsEmpty)
            {
                url += (url.Contains("?") ? "&" : "?") + QueryString;
            }

            // Initialize the request
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            // Misc
            request.Method          = Method.ToString().ToUpper();
            request.Credentials     = Credentials;
            request.Headers         = Headers.Headers;
            request.Accept          = Accept;
            request.Referer         = Referer;
            request.UserAgent       = UserAgent;
            request.Timeout         = (int)Timeout.TotalMilliseconds;
            request.CookieContainer = Cookies;
            if (!String.IsNullOrWhiteSpace(ContentType))
            {
                request.ContentType = ContentType;
            }
            if (!String.IsNullOrWhiteSpace(Host))
            {
                request.Host = Host;
            }

            // Handle various POST scenarios
            if (!String.IsNullOrWhiteSpace(Body))
            {
                // Set the length of the request body
                request.ContentLength = Body.Length;

                // Write the body to the request stream
                using (Stream stream = request.GetRequestStream()) {
                    stream.Write(Encoding.UTF8.GetBytes(Body), 0, Body.Length);
                }
            }
            else if (Method == SocialHttpMethod.Post || Method == SocialHttpMethod.Put || Method == SocialHttpMethod.Patch || Method == SocialHttpMethod.Delete)
            {
                // Make sure we have a POST data instance
                PostData = PostData ?? new SocialHttpPostData();

                if (PostData.IsMultipart)
                {
                    // Declare the boundary to be used for the multipart data
                    string boundary = Guid.NewGuid().ToString().Replace("-", "");

                    // Set the content type (including the boundary)
                    request.ContentType = "multipart/form-data; boundary=" + boundary;

                    // Write the multipart body to the request stream
                    using (Stream stream = request.GetRequestStream()) {
                        PostData.WriteMultipartFormData(stream, boundary);
                    }
                }
                else
                {
                    // Convert the POST data to an URL encoded string
                    string dataString = PostData.ToString();

                    // Set the content type
                    request.ContentType = "application/x-www-form-urlencoded";

                    // Set the length of the request body
                    request.ContentLength = dataString.Length;

                    // Write the body to the request stream
                    using (Stream stream = request.GetRequestStream()) {
                        stream.Write(Encoding.UTF8.GetBytes(dataString), 0, dataString.Length);
                    }
                }
            }

            // Call the callback
            if (callback != null)
            {
                callback(request);
            }

            // Get the response
            try {
                return(SocialHttpResponse.GetFromWebResponse(request.GetResponse() as HttpWebResponse, this));
            } catch (WebException ex) {
                if (ex.Status != WebExceptionStatus.ProtocolError)
                {
                    throw;
                }
                return(SocialHttpResponse.GetFromWebResponse(ex.Response as HttpWebResponse, this));
            }
        }
        /// <summary>
        /// Executes the request and returns the corresponding response as an instance of <see cref="SocialHttpResponse"/>.
        /// </summary>
        /// <param name="callback">Lets you specify a callback method for modifying the underlying <see cref="HttpWebRequest"/>.</param>
        /// <returns>Returns an instance of <see cref="SocialHttpResponse"/> representing the response.</returns>
        public SocialHttpResponse GetResponse(Action <HttpWebRequest> callback)
        {
            // Build the URL
            string url = Url;

            if (QueryString != null && !QueryString.IsEmpty)
            {
                url += (url.Contains("?") ? "&" : "?") + QueryString;
            }

            // Initialize the request
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            // Misc
            request.Method      = Method.ToString().ToUpper();
            request.Credentials = Credentials;
            request.Headers     = Headers.Headers;
            request.Accept      = Accept;
            request.Referer     = Referer;
            request.UserAgent   = UserAgent;
            request.Timeout     = (int)Timeout.TotalMilliseconds;
            if (!String.IsNullOrWhiteSpace(Host))
            {
                request.Host = Host;
            }

            // Add the request body (if a POST request)
            if (Method == SocialHttpMethod.Post)
            {
                if (IsMultipart)
                {
                    string boundary = Guid.NewGuid().ToString().Replace("-", "");
                    request.ContentType = "multipart/form-data; boundary=" + boundary;
                    using (Stream stream = request.GetRequestStream()) {
                        PostData.WriteMultipartFormData(stream, boundary);
                    }
                }
                else
                {
                    string dataString = PostData.ToString();
                    request.ContentType   = "application/x-www-form-urlencoded";
                    request.ContentLength = dataString.Length;
                    using (Stream stream = request.GetRequestStream()) {
                        stream.Write(Encoding.UTF8.GetBytes(dataString), 0, dataString.Length);
                    }
                }
            }

            // Call the callback
            if (callback != null)
            {
                callback(request);
            }

            // Get the response
            try {
                return(SocialHttpResponse.GetFromWebResponse(request.GetResponse() as HttpWebResponse, this));
            } catch (WebException ex) {
                if (ex.Status != WebExceptionStatus.ProtocolError)
                {
                    throw;
                }
                return(SocialHttpResponse.GetFromWebResponse(ex.Response as HttpWebResponse, this));
            }
        }