C#.NET程序员

后台HTTP请求

2018-03-30  本文已影响12人  wppeng
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

    }
}

上一篇下一篇

猜你喜欢

热点阅读