对接外网htpp的api的post,get接口封装类库

public class HttpHelper
    {

  public static string GetAsync(string url)
  {
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "GET";
request.ContentType = "application/json";
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
string encoding = response.ContentEncoding;
if (encoding == null || encoding.Length < 1)
{
encoding = "UTF-8"; //默认编码
}
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
return reader.ReadToEnd();
  }

        public string PostAsync(string url, string strContent)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/json";
            using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream()))
            {
                dataStream.Write(strContent);
                dataStream.Close();
            }
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            string encoding = response.ContentEncoding;
            if (encoding == null || encoding.Length < 1)
            {
                encoding = "UTF-8"; //默认编码  
            }
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
            return reader.ReadToEnd();
        }
}

https的请求,加入

ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;
在请求的前面
/// <summary>
        /// 发起GET请求
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string GetAsync(string url)
        {
            ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
            // ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;//SecurityProtocolType.Tls12 | *SecurityProtocolType.Tls11 | 
            ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "GET";
            request.ContentType = "application/json";
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            string encoding = response.ContentEncoding;
            if (encoding == null || encoding.Length < 1)
            {
                encoding = "UTF-8"; //默认编码
            }
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
            return reader.ReadToEnd();
        }

 

引用类库System.Net.Http;

public class JsonHelper
    {
        public static T DeserializeObject<T>(string szJson)
        {
            return JsonConvert.DeserializeObject<T>(szJson);
        }

        public static string SerializeObject(object obj)
        {
            return JsonConvert.SerializeObject(obj);
        }
    }

引用类库Newtonsoft.Json.dll; install-package newtonsoft.json v8.0.0.0

 

调用

 


posted @ 2018-10-29 16:52  世人皆萌  阅读(1030)  评论(0)    收藏  举报