HttpHelper

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web;

namespace YFAPICommon.Libs
{
    public class HttpHelper
    {
        static public string serverUrl = "http://192.168.0.86:8080/";
        public static JObject Post(string url, Dictionary<string, object> param)
        {
            using (HttpClient client = new HttpClient())
            {
                client.BaseAddress = new Uri(serverUrl);
                //OAuth2令牌
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "xxxx");
                //请求超时
                client.Timeout = new TimeSpan(5000);

                var httpContent = new StringContent(JsonConvert.SerializeObject(param));
                httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                var response = client.PostAsync(url, httpContent).Result;
                var responseValue = response.Content.ReadAsStringAsync().Result;
                if (response.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    return JObject.Parse(responseValue);
                }

                return null;
            }
        }

        public static JObject Get(string url)
        {
            using (HttpClient client = new HttpClient())
            {
                client.BaseAddress = new Uri(serverUrl);
                //OAuth2令牌
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "xxxx");
                //请求超时
                client.Timeout = new TimeSpan(5000);

                var response = client.GetAsync(url).Result;
                var responseValue = response.Content.ReadAsStringAsync().Result;
                if (response.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    return JObject.Parse(responseValue);
                }

                return null;
            }
        }
    }
}

 

 

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

namespace YFAPICommon.Controllers
{
    public class HttpHelper
    {
        public static JObject Post(string url, Dictionary<string, object> param)
        {
            string paramStr = JsonConvert.SerializeObject(param);
            byte[] data = System.Text.Encoding.UTF8.GetBytes(paramStr);
            var request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/json";

            Stream newStream = request.GetRequestStream();
            newStream.Write(data, 0, data.Length);
            newStream.Close();

            request.ServicePoint.Expect100Continue = false;
            request.ProtocolVersion = HttpVersion.Version11;

            var response = (HttpWebResponse)request.GetResponse();
            var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                return JObject.Parse(responseString);
            }

            return null;
        }



        public static JObject Get(string url)
        {
            var request = (HttpWebRequest)WebRequest.Create(url);
            var response = (HttpWebResponse)request.GetResponse();
            var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                return JObject.Parse(responseString);
            }
            return null;
        }
    }
}

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页