Пример #1
0
 /// <summary>
 ///     Execute a HEAD request and return the response. The supplied parameters  are URL encoded and sent as the query
 ///     string.
 /// </summary>
 /// <param name="path">Url resource</param>
 /// <param name="parameters">HTTP Payload</param>
 /// <returns>Response object</returns>
 public HttpResponse Head(string path, ParameterMap parameters)
 {
     return(Execute(new HttpHead(path, parameters)));
 }
Пример #2
0
 /// <summary>
 ///     Execute a DELETE request and return the response. The supplied parameters  are URL encoded and sent as the query
 ///     string.
 /// </summary>
 /// <param name="path">Url resource</param>
 /// <param name="parameters">HTTP Payload</param>
 /// <returns>Response object</returns>
 public HttpResponse Delete(string path, ParameterMap parameters)
 {
     return(Execute(new HttpDelete(path, parameters)));
 }
Пример #3
0
 /// <summary>
 ///     Execute a POST request with parameter map and return the response.
 /// </summary>
 /// <param name="path">Url resource</param>
 /// <param name="parameters">HTTP Payload</param>
 /// <returns>Response object</returns>
 public HttpResponse Post(string path, ParameterMap parameters)
 {
     return(Execute(new HttpPost(path, parameters)));
 }
Пример #4
0
        /// <summary>
        ///     This method uploads files onto the remote server using the POST method
        /// </summary>
        /// <param name="path">Resource Path</param>
        /// <param name="uploadFiles">Files to upload</param>
        /// <param name="parameters">Additional form data</param>
        /// <returns>HttpResponse Object</returns>
        /// <exception cref="HttpRequestException"></exception>
        /// <exception cref="Exception"></exception>
        public HttpResponse PostFiles(string path, UploadFile[] uploadFiles, ParameterMap parameters)
        {
            HttpResponse response = null;

            try {
                Connected = false;
                // Let us open the connection, prepare it for writing and reading data.
                HttpWebRequest urlConnection = OpenConnection(path);
                urlConnection.Accept           = Accept;
                urlConnection.KeepAlive        = true;
                urlConnection.ReadWriteTimeout = ReadWriteTimeout * 1000;
                urlConnection.Timeout          = ConnectionTimeout * 1000;
                urlConnection.Method           = "POST";
                urlConnection.Headers.Add("Accept-Charset", "UTF-8");
                AppendRequestHeaders(urlConnection);
                Connected = true;

                // Build form data to send to the server.
                // Let us set the form data
                NameValueCollection form = parameters.ToNameValueCollection();

                // upload the files
                HttpWebResponse resp = HttpUploadHelper.Upload(urlConnection, uploadFiles, form);
                using (resp) {
                    if (resp != null)
                    {
                        using (Stream inputStream = resp.GetResponseStream()) {
                            if (inputStream != null)
                            {
                                var buffer         = new byte[resp.ContentLength];
                                int bytesRead      = 0;
                                int totalBytesRead = bytesRead;
                                while (totalBytesRead < buffer.Length)
                                {
                                    bytesRead       = inputStream.Read(buffer, bytesRead, buffer.Length - bytesRead);
                                    totalBytesRead += bytesRead;
                                }
                                response = new HttpResponse(urlConnection.Address.AbsoluteUri, urlConnection.Headers, Convert.ToInt32(resp.StatusCode), buffer);
                            }
                        }
                    }
                }
                return(response);
            }
            catch (Exception e) {
                if (e.GetType() == typeof(WebException))
                {
                    var ex = e as WebException;
                    try {
                        response = ReadStreamError(ex);
                    }
                    catch (Exception ee) {
                        // Must catch IOException, but swallow to show first cause only
                        RequestLogger.Log(ee.StackTrace);
                    }
                    finally {
                        if (response == null || response.Status <= 0)
                        {
                            throw new HttpRequestException(e, response);
                        }
                    }
                }
                else
                {
                    // Different Exception
                    // Must catch IOException, but swallow to show first cause only
                    RequestLogger.Log(e.ToString());
                }
            }
            finally {
                // Here we log the Http Response
                if (RequestLogger.IsLoggingEnabled())
                {
                    RequestLogger.LogResponse(response);
                }
            }
            return(response);
        }