示例#1
0
        public static HeadersDto Values_To_HeaderDto(string name, string value)
        {
            HeadersDto headersDto = new HeadersDto();

            headersDto.Headers.Add(new HeaderDto {
                Name = name, Value = value
            });
            return(headersDto);
        }
 public string SendGetRequest(string uri, string login, string password, bool allowAutoRedirect, string contentType, HeadersDto headers)
 {
     try
     {
         HttpWebRequest  request         = GenerateRequest(uri, null, RequestMethod.Get, null, null, allowAutoRedirect, contentType, headers);
         HttpWebResponse response        = GetResponse(request);
         string          responseContent = GetResponseContent(response);
         return(responseContent);
     }
     catch (Exception ex)
     {
         _ilogger.LogError(ex.Message, ex);
         return(null);
     }
 }
        private HttpWebRequest GenerateRequest(string uri, string content, RequestMethod method, string login, string password, bool allowAutoRedirect, string contentType, HeadersDto headers)
        {
            if (uri == null)
            {
                throw new ArgumentNullException("uri");
            }
            if (string.IsNullOrEmpty(contentType))
            {
                contentType = "application/x-www-form-urlencoded";
            }
            // Create a request using a URL that can receive a post.
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);

            // Set the Method property of the request to POST.
            request.Method            = method.ToString();
            request.AllowAutoRedirect = allowAutoRedirect;
            //Default proxy does not work thats why set to null
            request.Proxy = null;
            //request.Credentials = new NetworkCredential("muhammads", "Leo4580@");
            //request.Proxy.Credentials = new NetworkCredential("muhammads", "Leo4580@");

            if (headers != null)
            {
                if (headers.Headers?.Count > 0)
                {
                    foreach (var header in headers.Headers)
                    {
                        request.Headers.Add(header.Name + " " + header.Value);
                    }
                }
            }

            // If login is empty use defaul credentials
            if (string.IsNullOrEmpty(login))
            {
                request.Credentials = CredentialCache.DefaultNetworkCredentials;
            }
            else
            {
                request.Credentials = new NetworkCredential(login, password);
            }
            if (method == RequestMethod.Post)
            {
                // Convert POST data to a byte array.
                byte[] byteArray = Encoding.UTF8.GetBytes(content);
                // Set the ContentType property of the WebRequest.
                request.ContentType = contentType;
                // Set the ContentLength property of the WebRequest.
                request.ContentLength = byteArray.Length;
                // Get the request stream.
                Stream dataStream = request.GetRequestStream();
                // Write the data to the request stream.
                dataStream.Write(byteArray, 0, byteArray.Length);
                // Close the Stream object.
                dataStream.Close();
            }
            return(request);
        }