c# 获取远程URL页面的内容

using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;
using System.Net;

namespace WeiXin.Models
{

    public class HttpHelper
    {

        /// <summary>
        /// 序列化URL
        /// </summary>
        /// <param name="dict"></param>
        /// <param name="IsAddJRSY_RING">是否增加cctkey</param>
        /// <returns></returns>
        public static string GetParameterUrl(Dictionary<string, string> dict, bool IsAddJRSY_RING)
        {
            if (IsAddJRSY_RING)
            {
                dict.Add("JRSY_RING", WebConfigHelper.GetAppSettingValue("cct_JRSY_RING")); ;
            }

            var list = new List<string>();

            foreach (var item in dict)
            {
                list.Add(item.Key + "=" + item.Value);
            }
            return string.Join("&", list);
        }

        /// <summary>
        /// 获取序列化后的值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="metodUrl"></param>
        /// <param name="dict"></param>
        /// <returns></returns>
        public static T doPostMethodToObj<T>(string meathod, Dictionary<string, string> dict)
        {
            string jsonBody = HttpHelper.GetParameterUrl(dict, true);
            string metodUrl = WebConfigHelper.GetAppSettingValue("cct_Server_Url") + meathod;
            return doPostMethodToObj<T>(metodUrl, jsonBody);
        }


        /// <summary>
        /// REST @POST 方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="metodUrl"></param>
        /// <param name="jsonBody"></param>
        /// <returns></returns>
        public static T doPostMethodToObj<T>(string metodUrl, string jsonBody)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(metodUrl);

            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";

            var stream = request.GetRequestStream();
            using (var writer = new StreamWriter(stream))
            {
                writer.Write(jsonBody);
                writer.Flush();
            }
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            string json = getResponseString(response);
            return JsonConvert.DeserializeObject<T>(json);
        }



        /// <summary>
        /// REST @Get 方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="metodUrl"></param>
        /// <param name="jsonBody"></param>
        /// <returns></returns>
        public static T doGetMethodToObj<T>(string metodUrl)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(metodUrl);

            request.Credentials = new System.Net.NetworkCredential("user", "pwd", "domain");
            request.Proxy = new WebProxy("192.168.72.237:8080", true, new string[] { }, request.Credentials);

            request.Method = "get";
            request.ContentType = "application/json;charset=UTF-8";
            HttpWebResponse response = null;
            try
            {
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException e)
            {
                response = (HttpWebResponse)e.Response;
                return default(T);
            }
            string json = getResponseString(response);
            return JsonConvert.DeserializeObject<T>(json);
        }



        /// <summary>
        ///  将 HttpWebResponse 返回结果转换成 string
        /// </summary>
        /// <param name="response"></param>
        /// <returns></returns>
        private static string getResponseString(HttpWebResponse response)
        {
            string json = null;
            using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("UTF-8")))
            {
                json = reader.ReadToEnd();
            }
            return json;
        }


    }
}

猜你喜欢

转载自blog.csdn.net/a22698488/article/details/75009943