后台HTTP请求

作者: wppeng | 来源:发表于2018-03-30 11:36 被阅读12次
    using System;
    using System.IO;
    using System.Net;
    using System.Text;
    
    namespace Common
    {
        public class Http
        {
            #region Get请求
            /// <summary>   
            /// HTTP GET方式请求数据.     
            /// </summary>      
            /// <param name="url">URL.</param>      
            /// <returns></returns>     
            public static string HttpGet(string url)
            {
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
                request.Method = "GET";
                request.Accept = "*/*";
                request.Timeout = 15000;
                request.AllowAutoRedirect = false;
    
                WebResponse response = null;
                string responseStr = null;
    
                try
                {
                    response = request.GetResponse();
    
                    if (response != null)
                    {
                        StreamReader reader =new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                        responseStr = reader.ReadToEnd();
                        reader.Close();
                    }
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    request = null;
                    response = null;
                }
    
                return responseStr;
            }
            #endregion
    
            #region POST请求
            /// <summary>   
            /// HTTP POST方式请求数据     
            /// </summary>      
            /// <param name="url">URL.</param>      
            /// <returns></returns>     
            public static string HttpPost(string url)
            {
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.Accept = "*/*";
                request.Timeout = 15000;
                request.AllowAutoRedirect = false;
    
                WebResponse response = null;
                string responseStr = null;
    
                try
                {
                    response = request.GetResponse();
                    if (response != null)
                    {
                        StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                        responseStr = reader.ReadToEnd();
                        reader.Close();
                    }
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    request = null;
                    response = null;
                }
    
                return responseStr;
            }
            /// <summary>    
            /// HTTP POST方式请求数据    
            /// </summary>    
            /// <param name="url">URL.</param>    
            /// <param name="Data">POST的数据</param>    
            /// <returns></returns>  
            public static  string HttpPost(string url, string Data)
            {
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                //request.ContentType = "application/json";
                request.Accept = "*/*";
                request.Timeout = 15000;
                request.AllowAutoRedirect = false;
    
                StreamWriter requestStream = null;
                WebResponse response = null;
                string responseStr = null;
    
                try
                {
                    requestStream = new StreamWriter(request.GetRequestStream(), Encoding.UTF8);
                    requestStream.Write(Data);
                    requestStream.Close();
    
                    response = request.GetResponse();
                    if (response != null)
                    {
                        StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                        responseStr = reader.ReadToEnd();
                       // File.WriteAllText(System.Web.HttpContext.Current.Server.MapPath("~//weixin/Weixin/") + @"\test.txt", responseStr);
                        reader.Close();
                    }
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    request = null;
                    requestStream = null;
                    response = null;
                }
    
                return responseStr;
            }
            /// <summary>    
            /// HTTP POST方式请求数据    
            /// </summary>    
            /// <param name="url">URL.</param>    
            /// <param name="Data">POST的数据</param> 
            /// <param name="type">请求类型(json,xml,param)</param>
            /// <returns></returns>  
            public static string HttpPost(string url, string Data, string type)
            {
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
                request.Method = "POST";
                switch (type)
                {
                    case "xml":
                        request.ContentType = "text/xml; Charset=utf-8";
                        break;
                    case "json":
                        request.ContentType = "application/json";
                        break;
                    default:
                        request.ContentType = "application/x-www-form-urlencoded";
                        break;
                }
                request.Headers.Add("Accept-Language: zh-CN");
                request.Accept = "*/*";
                request.Timeout = 15000;
                request.AllowAutoRedirect = false;
    
                StreamWriter requestStream = null;
                WebResponse response = null;
                string responseStr = null;
    
                try
                {
                    requestStream = new StreamWriter(request.GetRequestStream(), Encoding.UTF8);
                    requestStream.Write(Data);
                    requestStream.Close();
    
                    response = request.GetResponse();
                    if (response != null)
                    {
                        StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                        responseStr = reader.ReadToEnd();
    
                        reader.Close();
                    }
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    request = null;
                    requestStream = null;
                    response = null;
                }
    
                return responseStr;
            }
            #endregion
    
        }
    }
    
    

    相关文章

      网友评论

        本文标题:后台HTTP请求

        本文链接:https://www.haomeiwen.com/subject/uhabcftx.html