예제 #1
0
        private static string GetInternal(
            string url,
            Dictionary <string, string> headers,
            bool gzip,
            int timeout,
            NameValueCollection headerCollection = null,
            string userAgent = "")
        {
            HttpWebRequest httpWebRequest = WebRequest.Create(url) as HttpWebRequest;

            httpWebRequest.Method = "GET";
            if (timeout != 0)
            {
                httpWebRequest.Timeout = timeout;
            }
            if (gzip)
            {
                httpWebRequest.AutomaticDecompression = DecompressionMethods.GZip;
                httpWebRequest.Headers.Add(HttpRequestHeader.AcceptEncoding, nameof(gzip));
            }
            if (headerCollection != null)
            {
                httpWebRequest.Headers.Add(headerCollection);
            }
            if (!string.IsNullOrEmpty(userAgent))
            {
                httpWebRequest.UserAgent = userAgent;
            }
            if (headers != null)
            {
                foreach (KeyValuePair <string, string> header in headers)
                {
                    httpWebRequest.Headers.Set(StringUtils.GetControlCharFreeString(header.Key), StringUtils.GetControlCharFreeString(header.Value));
                }
            }
            using (HttpWebResponse response = httpWebRequest.GetResponse() as HttpWebResponse)
            {
                Logger.Debug("Response StatusCode:" + response.StatusCode.ToString());
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8))
                        return(streamReader.ReadToEnd());
                }
            }
        }
예제 #2
0
        private static string PostInternal(
            string url,
            Dictionary <string, string> data,
            Dictionary <string, string> headers,
            bool gzip,
            int timeout,
            NameValueCollection headerCollection,
            string userAgent)
        {
            HttpWebRequest httpWebRequest = WebRequest.Create(url) as HttpWebRequest;

            httpWebRequest.Method = "POST";
            if (timeout != 0)
            {
                httpWebRequest.Timeout = timeout;
            }
            if (gzip)
            {
                httpWebRequest.AutomaticDecompression = DecompressionMethods.GZip;
                httpWebRequest.Headers.Add(HttpRequestHeader.AcceptEncoding, nameof(gzip));
            }
            if (headerCollection != null)
            {
                httpWebRequest.Headers.Add(headerCollection);
            }
            if (!string.IsNullOrEmpty(userAgent))
            {
                httpWebRequest.UserAgent = userAgent;
            }
            if (headers != null)
            {
                foreach (KeyValuePair <string, string> header in headers)
                {
                    httpWebRequest.Headers.Set(StringUtils.GetControlCharFreeString(header.Key), StringUtils.GetControlCharFreeString(header.Value));
                }
            }
            if (data == null)
            {
                data = new Dictionary <string, string>();
            }
            byte[] bytes = Encoding.UTF8.GetBytes(StringUtils.Encode(data));
            httpWebRequest.ContentType   = "application/x-www-form-urlencoded";
            httpWebRequest.ContentLength = (long)bytes.Length;
            using (Stream requestStream = httpWebRequest.GetRequestStream())
            {
                requestStream.Write(bytes, 0, bytes.Length);
                using (HttpWebResponse response = httpWebRequest.GetResponse() as HttpWebResponse)
                {
                    Logger.Debug("Response StatusCode:" + response.StatusCode.ToString());
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        using (StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8))
                            return(streamReader.ReadToEnd());
                    }
                }
            }
        }