C# HTTP请求对外接口、第三方接口公用类
/// <summary> /// 网络数据请求公共函数 /// </summary> public class HttpWebRequestCommon { #region 根据HTTP协议请求接口,不携带参数 /// <summary> /// 根据HTTP协议请求接口,不携带参数 /// </summary> /// <param name="url">请求的第三方接口地址</param> /// <returns>接口返回的数据</returns> public static string HttpRequest(string url) { //string returnData = null; string ret = string.Empty; try { HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(url); webReq.Method = "POST"; webReq.ContentType = "application/json"; Stream postData = webReq.GetRequestStream(); postData.Close(); HttpWebResponse webResp = (HttpWebResponse)webReq.GetResponse(); StreamReader sr = new StreamReader(webResp.GetResponseStream(), Encoding.UTF8); ret = sr.ReadToEnd(); } catch (Exception ex) { //LogManager.LogInstance.WriteLog("时间:" + DateTime.Now + "/n 请求出错原因" + ex.ToString()); } return ret; } #endregion #region 根据HTTP协议请求第三方接口数据,携带参数方式在地址后使用占位符拼接参数。传递格式请参照下个函数 /// <summary> /// 根据HTTP协议请求第三方接口数据,携带参数方式在地址后使用占位符拼接参数 /// </summary> /// <param name="PostUrl">请求的第三方地址</param> /// <param name="postData">在地址后拼接的参数集合,传递格式请参照调用示范</param> /// <returns>接口返回的数据格式</returns> public static string HttpRequestUrl(string PostUrl, byte[] postData) { HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(PostUrl); myRequest.Method = "POST"; myRequest.ContentType = "application/x-www-form-urlencoded"; myRequest.ContentLength = postData.Length; Stream newStream = myRequest.GetRequestStream(); // Send the data. newStream.Write(postData, 0, postData.Length); newStream.Flush(); newStream.Close(); string msg = string.Empty; HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); if (myResponse.StatusCode == HttpStatusCode.OK) { StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); msg = reader.ReadToEnd(); } return msg; } #endregion #region 根据HTTP协议请求第三方接口数据,携带参数方式在地址后使用占位符拼接参数。传递格式请参照下个函数 /// <summary> /// 根据HTTP协议请求第三方接口数据,携带参数方式在地址后使用占位符拼接参数 /// </summary> /// <param name="PostUrl">请求的第三方地址</param> /// <param name="postData">在地址后拼接的参数集合,传递格式请参照调用示范</param> /// <returns>接口返回的数据格式</returns> public static string HttpRequestUrl(string PostUrl, byte[] postData, string POSTisGET) { HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(PostUrl); myRequest.Method = POSTisGET; myRequest.ContentType = "application/x-www-form-urlencoded"; myRequest.ContentLength = postData.Length; Stream newStream = myRequest.GetRequestStream(); // Send the data. newStream.Write(postData, 0, postData.Length); newStream.Flush(); newStream.Close(); string msg = string.Empty; HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); if (myResponse.StatusCode == HttpStatusCode.OK) { StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); msg = reader.ReadToEnd(); } return msg; } #endregion #region 上面HttpRequestUrl函数的调用示范,url存请求地址。参数通过字符串占位符拼接 //string url = "https://api.weixin.qq.com/cgi-bin/token"; //string postStrTpl = "grant_type=client_credential&appid={0}&secret={1}"; //UTF8Encoding encoding = new UTF8Encoding(); //byte[] postData = encoding.GetBytes(string.Format(postStrTpl, "参数一", "参数二")); //string retString = HttpWebRequestCommon.RequestUrl(url, postData); #endregion #region 根据HTTP协议请求第三方接口,携带XML参数 /// <summary> /// 根据HTTP协议请求第三方接口,携带XML参数 /// </summary> /// <param name="url">请求地址</param> /// <param name="data">携带的XML数据</param> /// <returns>接口返回的数据</returns> public static string HttpRequestXml(string url, string data) { //string returnData = null; string ret = string.Empty; try { byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes(data); HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(url); webReq.Method = "POST"; webReq.ContentType = "application/x-www-form-urlencoded"; webReq.ContentLength = buffer.Length; Stream postData = webReq.GetRequestStream(); postData.Write(buffer, 0, buffer.Length); postData.Close(); HttpWebResponse webResp = (HttpWebResponse)webReq.GetResponse(); StreamReader sr = new StreamReader(webResp.GetResponseStream(), Encoding.UTF8); ret = sr.ReadToEnd(); } catch (Exception ex) { //LogManager.LogInstance.WriteLog("时间:" + DateTime.Now + "/n 请求出错原因" + ex.ToString()); } return ret; } #endregion #region 根据HTTP协议请求第三方接口,携带Json参数 /// <summary> /// 根据HTTP协议请求第三方接口,携带Json参数。 /// </summary> /// <param name="url">请求的第三方地址</param> /// <param name="content">Post提交数据内容(utf-8编码的)</param> /// <returns>接口返回的数据</returns> public static string HttpRequestJson(string url, string content) { string result = ""; //ServicePointManager.ServerCertificateValidationCallback += (s, cert, chain, sslPolicyErrors) => true; //ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Method = "POST"; req.ContentType = "application/json"; #region 添加Post 参数 byte[] data = Encoding.UTF8.GetBytes(content); req.ContentLength = data.Length; using (Stream reqStream = req.GetRequestStream()) { reqStream.Write(data, 0, data.Length); reqStream.Close(); } #endregion HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); Stream stream = resp.GetResponseStream(); //获取响应内容 using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { result = reader.ReadToEnd(); } return result; } #endregion }