Exemplo n.º 1
0
        /// <summary>
        /// HttpWebRequest 用GET方法访问指定URI
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="uri">请求发送到的 URI。</param>
        /// <param name="headers">请求的头部信息</param>
        /// <returns></returns>
        public static T HttpGet <T>(string uri, IDictionary <string, string> headers = null)
        {
#if netcore
            if (uri.StartsWith("https", StringComparison.OrdinalIgnoreCase))
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.SystemDefault | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
            }
#endif
#if net45
            if (uri.StartsWith("https", StringComparison.OrdinalIgnoreCase))
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
            }
#endif
#if net40
            if (uri.StartsWith("https", StringComparison.OrdinalIgnoreCase))
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
            }
#endif

            HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest;
            request.Method      = "GET";
            request.ContentType = "text/json";
            if (headers != null)
            {
                foreach (var kv in headers)
                {
                    request.Headers.Add(kv.Key, kv.Value);
                }
            }


            StreamReader reader = null;
            Stream       stream = null;
            try
            {
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                stream = response.GetResponseStream();
                reader = new StreamReader(stream);

                string line  = reader.ReadToEnd();
                T      value = SerializeHelper.DeserializeFromJson <T>(line);
                return(value);
            }
            catch (WebException we)
            {
                WebHelper.ThrowWebException(we);
                throw;
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
                if (reader != null)
                {
                    reader.Close();
                }
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// 设置Cookie
 /// </summary>
 /// <param name="key">cookie键</param>
 /// <param name="value">cookie值</param>
 /// <param name="expires">过期时间,分钟为单位,默认一天</param>
 /// <param name="encrypt">是否加密cookie</param>
 public static void SetCookie(string key, string value, int expires = 1440, bool encrypt = true)
 {
     WebHelper.SetCookie(key, value, DateTime.Now.AddMinutes(expires));
 }
Exemplo n.º 3
0
        /// <summary>
        /// HttpWebRequest 用POST方法访问指定URI
        /// </summary>
        public static T HttpPost <T>(string uri, string content, IDictionary <string, string> headers = null, string contentType = "application/json", int?timeout = null)
        {
            //application/x-www-form-urlencoded
#if netcore
            if (uri.StartsWith("https", StringComparison.OrdinalIgnoreCase))
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.SystemDefault | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
            }
#endif
#if net45
            if (uri.StartsWith("https", StringComparison.OrdinalIgnoreCase))
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
            }
#endif
#if net40
            if (uri.StartsWith("https", StringComparison.OrdinalIgnoreCase))
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
            }
#endif

            HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest;
            if (timeout != null)
            {
                request.Timeout = timeout.Value;
            }
            request.Method        = "POST";
            request.ContentType   = contentType;
            request.ContentLength = 0;
            if (headers != null)
            {
                foreach (var kv in headers)
                {
                    request.Headers.Add(kv.Key, kv.Value);
                }
            }

            if (!string.IsNullOrEmpty(content))
            {
                byte[] sndBytes = Encoding.UTF8.GetBytes(content);
                request.ContentLength = sndBytes.Length;

                Stream rs = request.GetRequestStream();
                rs.Write(sndBytes, 0, sndBytes.Length);
                rs.Close();
            }


            StreamReader reader = null;
            Stream       stream = null;
            try
            {
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                stream = response.GetResponseStream();
                reader = new StreamReader(stream);

                string line  = reader.ReadToEnd();
                T      value = SerializeHelper.DeserializeFromJson <T>(line);
                return(value);
            }
            catch (WebException we)
            {
                WebHelper.ThrowWebException(we);
                throw;
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
                if (reader != null)
                {
                    reader.Close();
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// HttpWebRequest 用POST方法访问指定URI<c>使用完记得调用Stream.Close方法</c>
        /// </summary>
        public static HttpWebResponse HttpResponse(string uri, string content, IDictionary <string, string> headers = null, string contentType = "application/json", int?timeout = null, Encoding encoding = null)
        {
            //application/x-www-form-urlencoded

            encoding = encoding ?? Encoding.UTF8;
#if netcore
            if (uri.StartsWith("https", StringComparison.OrdinalIgnoreCase))
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.SystemDefault | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
            }
#endif
#if net45
            if (uri.StartsWith("https", StringComparison.OrdinalIgnoreCase))
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
            }
#endif
#if net40
            if (uri.StartsWith("https", StringComparison.OrdinalIgnoreCase))
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
            }
#endif

            HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest;
            if (timeout != null)
            {
                request.Timeout = timeout.Value;
            }
            request.Method        = "POST";
            request.ContentType   = contentType;
            request.ContentLength = 0;
            if (headers != null)
            {
                foreach (var kv in headers)
                {
                    request.Headers.Add(kv.Key, kv.Value);
                }
            }

            if (!string.IsNullOrEmpty(content))
            {
                byte[] sndBytes = encoding.GetBytes(content);
                request.ContentLength = sndBytes.Length;

                Stream rs = request.GetRequestStream();
                rs.Write(sndBytes, 0, sndBytes.Length);
                rs.Close();
            }

            try
            {
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                return(response);
            }
            catch (WebException we)
            {
                WebHelper.ThrowWebException(we);
                throw;
            }
        }