当前位置: 首页 > 工具软件 > httphelper > 使用案例 >

C#HttpHelper.cs封装

毋举
2023-12-01
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;

namespace ModbusTCPIPWS.HttpPostMan
{
    class HttpHelperApi
    {
        public static class HttpHelper
        {
            /// <summary>  
            /// 创建GET方式的HTTP请求  
            /// </summary>  
            public static HttpWebResponse CreateGetHttpResponse(string url, CookieCollection cookies)
            {
                HttpWebRequest request = null;
                request = WebRequest.Create(url) as HttpWebRequest;
                request.Method = "GET";
                request.Proxy = null;

                if (cookies != null)
                {
                    request.CookieContainer = new CookieContainer();
                    request.CookieContainer.Add(cookies);
                }
                return request.GetResponse() as HttpWebResponse;
            }

            /// <summary>  
            /// 创建POST方式的HTTP请求  
            /// </summary>  
            public static HttpWebResponse CreatePostHttpResponse(string url, IDictionary<string, string> parameters, int timeout, string userAgent, CookieCollection cookies)
            {
                HttpWebRequest request = null;

                request = WebRequest.Create(url) as HttpWebRequest;
                request.Proxy = null;
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                if (timeout > 0)
                    request.Timeout = timeout;
                if (string.IsNullOrEmpty(userAgent))
                    request.UserAgent = userAgent;

                if (cookies != null)
                {
                    request.CookieContainer = new CookieContainer();
                    request.CookieContainer.Add(cookies);
                }
                //发送POST数据  
                if (!(parameters == null || parameters.Count == 0))
                {
                    StringBuilder buffer = new StringBuilder();
                    int i = 0;
                    foreach (string key in parameters.Keys)
                    {
                        if (i > 0)
                        {
                            buffer.AppendFormat("&{0}={1}", key, parameters[key]);
                        }
                        else
                        {
                            buffer.AppendFormat("{0}={1}", key, parameters[key]);
                            i++;
                        }
                    }
                    byte[] data = Encoding.UTF8.GetBytes(buffer.ToString());
                    using (Stream stream = request.GetRequestStream())
                    {
                        stream.Write(data, 0, data.Length);
                    }
                }
                string[] values = request.Headers.GetValues("Content-Type");
                return request.GetResponse() as HttpWebResponse;
            }

            public static HttpWebResponse CreatePostHttpResponse(string url, string jsonParam)
            {
                HttpWebRequest request = null;

                request = WebRequest.Create(url) as HttpWebRequest;
                request.Proxy = null;
                request.Method = "POST";
                request.ContentType = "application/json";


                byte[] data = Encoding.UTF8.GetBytes(jsonParam);
                request.ContentLength = data.Length;
                using (Stream stream = request.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                }

                return request.GetResponse() as HttpWebResponse;
            }

            /// <summary>
            /// 获取请求的数据
            /// </summary>
            public static string GetResponseString(HttpWebResponse webresponse)
            {
                using (Stream s = webresponse.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(s, Encoding.UTF8);
                    return reader.ReadToEnd();

                }
            }

        }
    }
}

调用方式:

  var jquery = HttpHelperApi.HttpHelper.GetResponseString(HttpHelperApi.HttpHelper.CreatePostHttpResponse(url, JsonAmmeter));
 类似资料: