Beispiel #1
0
        private HttpWebRequest CreateWebRequest(HttpMethod method, string url, NameValueCollection headers = null, CookieCollection cookies = null,
                                                string contentType = null, long contentLength = 0)
        {
            LastResponseInfo = null;

            HttpWebRequest request = UploadHelpers.CreateWebRequest(method, url, headers, cookies, contentType, contentLength);

            currentWebRequest = request;
            return(request);
        }
Beispiel #2
0
        protected HttpWebResponse GetResponse(HttpMethod method, string url, Stream data = null, string contentType = null, Dictionary <string, string> args = null,
                                              NameValueCollection headers = null, CookieCollection cookies          = null, bool allowNon2xxResponses = false)
        {
            IsUploading         = true;
            StopUploadRequested = false;

            try
            {
                url = URLHelpers.CreateQuery(url, args);

                long contentLength = 0;

                if (data != null)
                {
                    contentLength = data.Length;
                }

                HttpWebRequest request = UploadHelpers.CreateWebRequest(method, url, headers, cookies, contentType, contentLength);
                currentRequest = request;

                if (contentLength > 0)
                {
                    using (Stream requestStream = request.GetRequestStream())
                    {
                        if (!TransferData(data, requestStream))
                        {
                            return(null);
                        }
                    }
                }

                return((HttpWebResponse)request.GetResponse());
            }
            catch (WebException we) when(we.Response != null && allowNon2xxResponses)
            {
                // if we.Response != null, then the request was successful, but
                // returned a non-200 status code
                return(we.Response as HttpWebResponse);
            }
            catch (Exception e)
            {
                if (!StopUploadRequested)
                {
                    AddWebError(e, url);
                }
            }
            finally
            {
                currentRequest = null;
                IsUploading    = false;
            }

            return(null);
        }
Beispiel #3
0
        protected UploadResult SendRequestFileRange(string url, Stream data, string fileName, long contentPosition = 0, long contentLength = -1,
                                                    Dictionary <string, string> args = null, NameValueCollection headers = null, CookieCollection cookies = null, ResponseType responseType = ResponseType.Text,
                                                    HttpMethod method = HttpMethod.PUT)
        {
            UploadResult result = new UploadResult();

            IsUploading         = true;
            StopUploadRequested = false;

            try
            {
                url = URLHelpers.CreateQuery(url, args);

                if (contentLength == -1)
                {
                    contentLength = data.Length;
                }
                contentLength = Math.Min(contentLength, data.Length - contentPosition);

                string contentType = UploadHelpers.GetMimeType(fileName);

                if (headers == null)
                {
                    headers = new NameValueCollection();
                }
                long startByte  = contentPosition;
                long endByte    = startByte + contentLength - 1;
                long dataLength = data.Length;
                headers.Add("Content-Range", $"bytes {startByte}-{endByte}/{dataLength}");

                HttpWebRequest request = UploadHelpers.CreateWebRequest(method, url, headers, cookies, contentType, contentLength);
                currentRequest = request;

                using (Stream requestStream = request.GetRequestStream())
                {
                    if (!TransferData(data, requestStream, contentPosition, contentLength))
                    {
                        return(null);
                    }
                }

                using (WebResponse response = request.GetResponse())
                {
                    result.Response = UploadHelpers.ResponseToString(response, responseType);
                }

                result.IsSuccess = true;
            }
            catch (Exception e)
            {
                if (!StopUploadRequested)
                {
                    string response = AddWebError(e, url);

                    if (ReturnResponseOnError && e is WebException)
                    {
                        result.Response = response;
                    }
                }
            }
            finally
            {
                currentRequest = null;
                IsUploading    = false;

                if (VerboseLogs && !string.IsNullOrEmpty(VerboseLogsPath))
                {
                    WriteVerboseLog(url, args, headers, result.Response);
                }
            }

            return(result);
        }
Beispiel #4
0
        protected UploadResult SendRequestFile(string url, Stream data, string fileName, string fileFormName, Dictionary <string, string> args = null,
                                               NameValueCollection headers = null, CookieCollection cookies = null, ResponseType responseType = ResponseType.Text, HttpMethod method = HttpMethod.POST,
                                               string contentType          = UploadHelpers.ContentTypeMultipartFormData, string metadata = null)
        {
            UploadResult result = new UploadResult();

            IsUploading         = true;
            StopUploadRequested = false;

            try
            {
                string boundary = UploadHelpers.CreateBoundary();
                contentType += "; boundary=" + boundary;

                byte[] bytesArguments = UploadHelpers.MakeInputContent(boundary, args, false);
                byte[] bytesDataOpen;
                byte[] bytesDataDatafile = { };

                if (metadata != null)
                {
                    bytesDataOpen     = UploadHelpers.MakeFileInputContentOpen(boundary, fileFormName, fileName, metadata);
                    bytesDataDatafile = UploadHelpers.MakeFileInputContentOpen(boundary, fileFormName, fileName, null);
                }
                else
                {
                    bytesDataOpen = UploadHelpers.MakeFileInputContentOpen(boundary, fileFormName, fileName);
                }

                byte[] bytesDataClose = UploadHelpers.MakeFileInputContentClose(boundary);

                long contentLength = bytesArguments.Length + bytesDataOpen.Length + bytesDataDatafile.Length + data.Length + bytesDataClose.Length;

                HttpWebRequest request = UploadHelpers.CreateWebRequest(method, url, headers, cookies, contentType, contentLength);
                currentRequest = request;

                using (Stream requestStream = request.GetRequestStream())
                {
                    requestStream.Write(bytesArguments, 0, bytesArguments.Length);
                    requestStream.Write(bytesDataOpen, 0, bytesDataOpen.Length);
                    requestStream.Write(bytesDataDatafile, 0, bytesDataDatafile.Length);
                    if (!TransferData(data, requestStream))
                    {
                        return(null);
                    }
                    requestStream.Write(bytesDataClose, 0, bytesDataClose.Length);
                }

                using (WebResponse response = request.GetResponse())
                {
                    result.Response = UploadHelpers.ResponseToString(response, responseType);
                }

                result.IsSuccess = true;
            }
            catch (Exception e)
            {
                if (!StopUploadRequested)
                {
                    string response = AddWebError(e, url);

                    if (ReturnResponseOnError && e is WebException)
                    {
                        result.Response = response;
                    }
                }
            }
            finally
            {
                currentRequest = null;
                IsUploading    = false;

                if (VerboseLogs && !string.IsNullOrEmpty(VerboseLogsPath))
                {
                    WriteVerboseLog(url, args, headers, result.Response);
                }
            }

            return(result);
        }