TrakHound.Tools.Web.HTTP.SendData C# (CSharp) Method

SendData() private static method

private static SendData ( bool returnBytes, string method, string url, PostContentData postDatas = null, FileContentData fileDatas = null, HeaderData headers = null, string userAgent = null, NetworkCredential credential = null, ProxySettings proxySettings = null, int timeout = TIMEOUT, int maxAttempts = CONNECTION_ATTEMPTS, bool getResponse = true ) : ResponseInfo
returnBytes bool
method string
url string
postDatas PostContentData
fileDatas FileContentData
headers HeaderData
userAgent string
credential System.Net.NetworkCredential
proxySettings ProxySettings
timeout int
maxAttempts int
getResponse bool
return ResponseInfo
        private static ResponseInfo SendData(
            bool returnBytes,
            string method,
            string url,
            PostContentData[] postDatas = null,
            FileContentData[] fileDatas = null,
            HeaderData[] headers = null,
            string userAgent = null,
            NetworkCredential credential = null,
            ProxySettings proxySettings = null,
            int timeout = TIMEOUT,
            int maxAttempts = CONNECTION_ATTEMPTS,
            bool getResponse = true
            )
        {
            ResponseInfo result = null;

            int attempts = 0;
            bool success = false;
            string message = null;

            // Try to send data for number of connectionAttempts
            while (attempts < maxAttempts && !success)
            {
                attempts += 1;

                try
                {
                    // Create HTTP request and define Header info
                    var request = (HttpWebRequest)WebRequest.Create(url);

                    string boundary = String_Functions.RandomString(10);

                    request.Timeout = timeout;
                    request.ReadWriteTimeout = timeout;
                    if (method == "POST") request.ContentType = "multipart/form-data; boundary=" + boundary;
                    else request.ContentType = "application/x-www-form-urlencoded";

                    // Set the Method
                    request.Method = method;

                    // Set the UserAgent
                    if (userAgent != null) request.UserAgent = userAgent;
                    else request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";

                    // Add Header data to request stream (if present)
                    if (headers != null)
                    {
                        foreach (var header in headers)
                        {
                            request.Headers[header.Id] = header.Text;
                        }
                    }

                    // set NetworkCredentials
                    if (credential != null)
                    {
                        request.Credentials = credential;
                        request.PreAuthenticate = true;
                    }

                    // Get Default System Proxy (Windows Internet Settings -> Proxy Settings)
                    var proxy = WebRequest.GetSystemWebProxy();

                    // Get Custom Proxy Settings from Argument (overwrite default proxy settings)
                    if (proxySettings != null)
                    {
                        if (proxySettings.Address != null && proxySettings.Port > 0)
                        {
                            var customProxy = new WebProxy(proxySettings.Address, proxySettings.Port);
                            customProxy.BypassProxyOnLocal = false;
                            proxy = customProxy;
                        }
                    }

                    request.Proxy = proxy;

                    var bytes = new List<byte>();

                    // Add Post Name/Value Pairs
                    if (postDatas != null)
                    {
                        string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";

                        foreach (var postData in postDatas)
                        {
                            string formitem = string.Format(formdataTemplate, postData.Name, postData.Value);

                            bytes.AddRange(GetBytes("\r\n--" + boundary + "\r\n"));
                            bytes.AddRange(GetBytes(formitem));
                        }
                    }

                    // Add File data
                    if (fileDatas != null)
                    {
                        bytes.AddRange(GetFileContents(fileDatas, boundary));
                    }

                    if (bytes.Count > 0)
                    {
                        // Write Trailer Boundary
                        string trailer = "\r\n--" + boundary + "--\r\n";
                        bytes.AddRange(GetBytes(trailer));

                        var byteArray = bytes.ToArray();

                        // Write Data to Request Stream
                        request.ContentLength = byteArray.Length;

                        using (var requestStream = request.GetRequestStream())
                        {
                            requestStream.Write(byteArray, 0, byteArray.Length);
                        }
                    }

                    // Get Response Message from HTTP Request
                    if (getResponse)
                    {
                        result = new ResponseInfo();

                        using (var response = (HttpWebResponse)request.GetResponse())
                        {
                            // Get HTTP Response Body
                            using (var responseStream = response.GetResponseStream())
                            using (var memStream = new MemoryStream())
                            {
                                byte[] buffer = new byte[10240];

                                int read;
                                while ((read = responseStream.Read(buffer, 0, buffer.Length)) > 0)
                                {
                                    memStream.Write(buffer, 0, read);
                                }

                                result.Body = memStream.ToArray();

                                success = true;
                            }

                            var responseHeaders = new List<ReponseHeaderInfo>();

                            // Get HTTP Response Headers
                            foreach (var key in response.Headers.AllKeys)
                            {
                                var header = new ReponseHeaderInfo();
                                header.Key = key;
                                header.Value = response.Headers.Get(key);

                                responseHeaders.Add(header);
                            }

                            result.Headers = responseHeaders.ToArray();
                        }
                    }
                    else success = true;
                }
                catch (WebException wex) { message = wex.Message; }
                catch (Exception ex) { message = ex.Message; }

                if (!success) System.Threading.Thread.Sleep(500);
            }

            if (!success) Logger.Log("Send :: " + attempts.ToString() + " Attempts :: URL = " + url + " :: " + message);

            return result;
        }

Same methods

HTTP::SendData ( string method, string url, PostContentData postDatas = null, FileContentData fileDatas = null, HeaderData headers = null, string userAgent = null, NetworkCredential credential = null, ProxySettings proxySettings = null, int timeout = TIMEOUT, int maxAttempts = CONNECTION_ATTEMPTS, bool getResponse = true ) : string