HttpRequestUtil类

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
  public class HttpRequestUtil
    {
        public string SendHttpGetRequest(string strUrl, Dictionary<string, string> dic)
        {
            if (dic != null && dic.Count > 0)
            {
                if (strUrl.Contains("?"))
                {
                    foreach (var item in dic)
                    {
                        strUrl += "&" + item.Key + "=" + item.Value;
                    }
                }
                else
                {
                    strUrl += "?";
                    foreach (var item in dic)
                    {
                        strUrl += item.Key + "=" + item.Value + "&";
                    }
                    strUrl = strUrl.Substring(0, strUrl.Length - 1);
                }

            }
            return SendHttpGetRequest(strUrl);
        }
        /// <summary>
        /// 模拟httpget请求
        /// </summary>
        /// <param name="strUrl"></param>
        /// <param name="strGetData"></param>
        /// <returns></returns>
        public string SendHttpGetRequest(string strUrl)
        {
            string strJson = string.Empty;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strUrl);
                request.Method = "get";
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream stream = response.GetResponseStream();
                StreamReader reader = new StreamReader(stream);
                strJson = reader.ReadToEnd();
                reader.Close();
                return strJson;
            }
            catch (Exception ex)
            {
                LogHelper.WriteLogs("发送get请求出错",ex);
                return strJson;
            }

        }

        public string SendHttpPostRequest(string strUrl, string strPostData)
        {
            string ret = string.Empty;
            try
            {
                byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(strPostData); //转化
                HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(strUrl));
                webReq.Method = "POST";
                webReq.ContentType = "application/x-www-form-urlencoded";

                webReq.ContentLength = byteArray.Length;
                Stream newStream = webReq.GetRequestStream();
                newStream.Write(byteArray, 0, byteArray.Length);//写入参数
                newStream.Close();
                HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                ret = sr.ReadToEnd();
                sr.Close();
                response.Close();
                newStream.Close();
            }
            catch (Exception ex)
            {
                string strContent = "请求地址:" + strUrl + " postData:" + strPostData + "
异常信息:" + ex.Message + ex.StackTrace;
            }
            return ret;
        }
        
        public string SendHttpPostRequest(string strUrl, string strPostData, Encoding dataEncode)
        {
            string ret = string.Empty;
            try
            {
                byte[] byteArray = dataEncode.GetBytes(strPostData); //转化
                HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(strUrl));
                webReq.Method = "POST";
                webReq.ContentType = "application/x-www-form-urlencoded";

                webReq.ContentLength = byteArray.Length;
                Stream newStream = webReq.GetRequestStream();
                newStream.Write(byteArray, 0, byteArray.Length);//写入参数
                newStream.Close();
                HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                ret = sr.ReadToEnd();
                sr.Close();
                response.Close();
                newStream.Close();
            }
            catch (Exception ex)
            {
                string strContent = "请求地址:" + strUrl + " postData:" + strPostData + "
异常信息:" + ex.Message + ex.StackTrace;
            }
            return ret;
        }

        public string SendHttpPostRequest(string strUrl, string strPostData, string strContentType)
        {
            string ret = string.Empty;
            try
            {
                byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(strPostData); //转化
                HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(strUrl));
                webReq.Method = "POST";
                if (string.IsNullOrEmpty(strContentType))
                {
                    strContentType = "application/json";
                }
                webReq.ContentType = strContentType;

                webReq.ContentLength = byteArray.Length;
                Stream newStream = webReq.GetRequestStream();
                newStream.Write(byteArray, 0, byteArray.Length);//写入参数
                newStream.Close();
                HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default);
                ret = sr.ReadToEnd();
                sr.Close();
                response.Close();
                newStream.Close();
            }
            catch (Exception ex)
            {
                string strContent = "请求地址:" + strUrl + " postData:" + strPostData + "
异常信息:" + ex.Message + ex.StackTrace;
            }
            return ret;
        }
    }
© 版权声明 文章版权归作者所有,若需转载,请在显著位置标志该文章地址。
原文地址:https://www.cnblogs.com/luchenglong/p/13667813.html