HttpWebRequest调用接口
Posted 那一缕阳光
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HttpWebRequest调用接口相关的知识,希望对你有一定的参考价值。
/// <summary> /// Http 请求 通用来 /// </summary> public abstract class HttpRequest { public static string HttpAccept_JSON_UTF8 = "application/json;charset=UTF-8"; public static string HttpAccept_XML = "application/xml;"; public static int? reqest_timeout = 15000; /// <summary> /// 发起 HttpGet 请求 /// </summary> /// <param name="requestUriString"></param> /// <param name="content"></param> /// <param name="timeout">请求超时前等待的毫秒数。默认值是 15,000 毫秒(15 秒)。</param> /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param> /// <param name="httpAccept">接受的数据类型</param> /// <returns></returns> public static string HttpGet(string requestUriString,string content=null,int timeout= 0, string contentType =null, string httpAccept=null)//get方式提交数据 { //计数器用于识别请求 long num = DateTime.Now.Ticks; contentType = contentType ?? "application/x-www-form-urlencoded"; httpAccept = httpAccept ?? HttpAccept_XML; string url = requestUriString; if (url.IndexOf("?") <0) { url += "?"; } url = url + content; HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); request.Method = "GET"; request.ContentType = contentType; request.Accept = httpAccept ?? HttpAccept_XML; request.Timeout = timeout > 0 ? timeout : reqest_timeout.Value; request.AllowAutoRedirect = false; WebResponse response = null; string responseStr = null; try { response = request.GetResponse(); if (response != null) { StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); responseStr = reader.ReadToEnd(); reader.Close(); } } catch (Exception ex) { //LogService.Error($"HttpGet 请求出错 url:{url}", ex); } finally { request = null; response = null; } return responseStr; } /// <summary> /// HTTP POST方式请求数据 /// </summary> /// <param name="url">URL</param> /// <param name="content">POST的数据</param> /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param> /// <param name="httpAccept ">接受的数据类型</param> /// <param name="timeout">超时时间毫秒</param> /// <returns>请求返回数据</returns> public static string HttpPost(string url, string content, int? timeout = null, string contentType = null, string httpAccept =null) { //计数器用于识别请求 long num = DateTime.Now.Ticks; contentType = contentType ?? "application/x-www-form-urlencoded"; httpAccept = httpAccept ?? HttpAccept_XML; timeout = timeout?? reqest_timeout; HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); request.Method = "POST"; request.ContentType = contentType; request.Accept = httpAccept ; request.Timeout = timeout.Value; request.AllowAutoRedirect = false; StreamWriter requestStream = null; WebResponse response = null; string responseStr = null; try { requestStream = new StreamWriter(request.GetRequestStream()); requestStream.Write(content); requestStream.Close(); response = request.GetResponse(); if (response != null) { StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); responseStr = reader.ReadToEnd(); reader.Close(); } } catch (Exception ex) { //LogService.Error($"HttpPost 请求出错 URL:{url} Content:{content}", ex); } finally { request = null; requestStream = null; response = null; } return responseStr; } }
方法调用:
string url="";//接口地址 string content="";//参数
var dic = new Dictionary<string, string>();
dic.Add("appid", appId);
dic.Add("timestamp", timeStamp);
dic.Add("sign", sign);
string jsonResult = HttpRequest.HttpPost(url, content, 15000, "application/json", "application/json", dic);
string jsonResult = HttpRequest.HttpGet(url, content, 0, "application/json", null, dic);
以上是关于HttpWebRequest调用接口的主要内容,如果未能解决你的问题,请参考以下文章
使用HttpWebRequest时,为什么我在某些链接上出现“(304)Not Modified”错误?
HttpClient 等效于 HttpWebRequest 的 ServicePoint.MaxIdleTime 属性