Ejemplo n.º 1
0
        protected string SendRequestMultiPart(string url, Dictionary <string, string> args, NameValueCollection headers = null, CookieCollection cookies = null,
                                              ResponseType responseType = ResponseType.Text, HttpMethod method = HttpMethod.POST)
        {
            string boundary    = UploadHelpers.CreateBoundary();
            string contentType = UploadHelpers.ContentTypeMultipartFormData + "; boundary=" + boundary;

            byte[] data = UploadHelpers.MakeInputContent(boundary, args);

            using (MemoryStream stream = new MemoryStream())
            {
                stream.Write(data, 0, data.Length);

                using (HttpWebResponse webResponse = GetResponse(method, url, stream, contentType, null, headers, cookies))
                {
                    string response = UploadHelpers.ResponseToString(webResponse, responseType);

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

                    return(response);
                }
            }
        }
Ejemplo n.º 2
0
        private string AddWebError(Exception e, string url)
        {
            string response = null;

            if (Errors != null && e != null)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("Message:");
                sb.AppendLine(e.Message);

                if (!string.IsNullOrEmpty(url))
                {
                    sb.AppendLine();
                    sb.AppendLine("Request URL:");
                    sb.AppendLine(URLHelpers.RemoveQuery(url));
                }

                if (e is WebException webException)
                {
                    try
                    {
                        WebResponse res = webException.Response;
                        using (res)
                        {
                            response = UploadHelpers.ResponseToString(res);

                            if (!string.IsNullOrEmpty(response))
                            {
                                sb.AppendLine();
                                sb.AppendLine("Response:");
                                sb.AppendLine(response);
                            }
                        }
                    }
                    catch (Exception nested)
                    {
                        DebugHelper.WriteException(nested, "AddWebError() WebException handler");
                    }
                }

                sb.AppendLine();
                sb.AppendLine("Stack trace:");
                sb.AppendLine(e.StackTrace);

                string errorText = sb.ToString().Trim();
                Errors.Add(errorText);
                DebugHelper.WriteLine("Error:\r\n" + errorText);
            }

            return(response);
        }
Ejemplo n.º 3
0
        protected string SendRequest(HttpMethod method, string url, Stream data, string contentType = null, Dictionary <string, string> args = null, NameValueCollection headers = null,
                                     CookieCollection cookies = null, ResponseType responseType = ResponseType.Text)
        {
            using (HttpWebResponse webResponse = GetResponse(method, url, data, contentType, args, headers, cookies))
            {
                string response = UploadHelpers.ResponseToString(webResponse, responseType);

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

                return(response);
            }
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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);
        }