.net后台调用API接口
//引用using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Net;using System.Text;using System.Web;using Newtonsoft.Json.Linq;using Newtonsoft.Json;usin...
·
//引用
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using ProvinceAPI.Model;
using System.Net.Security;
private void chehui()
{
string token = "";
string Year = "2019";
string RegionID = "0";
int DataType = 2;
token = APIHelp.GetAccessToken().Data;
string result = APIHelp.Post("外网url", token, RegionID, Year, DataType);
Response.Write(result);
}
const string api_url = "http://外网url/";
const string api_appid = "e453467af0c311e9";
const string app_secret = "e453467af0c311e99d49fefcfe183302";
public static AccessToken GetAccessToken()
{
string url = api_url + "Authorization/GetAccessToken?appID=" + api_appid + "&appSecret=" + app_secret;
string result = GetHttpResponse(url, 5000);
JObject obj = (JObject)JsonConvert.DeserializeObject(result);
AccessToken token = new AccessToken();
token.IsSuccess = (bool)obj["IsSuccess"];
token.Message = (string)obj["Message"];
token.Data = (string)obj["Data"];
return token;
}
public static string Post(string url, string accesstoken, string RegionID, string Year, int DataType)
{
System.GC.Collect();//垃圾回收,回收没有正常关闭的http连接
string result = "";//返回结果
HttpWebRequest request = null;
HttpWebResponse response = null;
Stream reqStream = null;
try
{
//设置最大连接数
ServicePointManager.DefaultConnectionLimit = 200;
/***************************************************************
* 下面设置HttpWebRequest的相关属性
* ************************************************************/
request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
//这个在Post的时候,一定要加上,如果服务器返回错误,他还会继续再去请求,不会使用之前的错误数据,做返回数据
request.ServicePoint.Expect100Continue = false;
//设置POST的数据类型和长度
request.ContentType = "application/x-www-form-urlencoded;charset=gb2312";
StringBuilder buffer = new StringBuilder();
buffer.AppendFormat("&{0}={1}", "AccessToken", accesstoken);
buffer.AppendFormat("&{0}={1}", "RegionID", RegionID);
buffer.AppendFormat("&{0}={1}", "Year", Year);
buffer.AppendFormat("&{0}={1}", "DataType", DataType);
//StringBuilder buffer = new StringBuilder();
//buffer.Append("{\"AccessToken\":\"" + accesstoken + "\",");
//buffer.Append("\"RegionID\":\"" + RegionID + "\",");
//buffer.Append("\"Year\":\"" + Year + "\",");
//buffer.Append("\"DataType\":" + DataType + "}");
//上面两种传参数的方式,本人试了json不行就用键值对的。
byte[] data = System.Text.Encoding.UTF8.GetBytes(buffer.ToString());
request.ContentLength = data.Length;
//往服务器写入数据
reqStream = request.GetRequestStream();
reqStream.Write(data, 0, data.Length);
reqStream.Close();
//获取服务端返回
response = (HttpWebResponse)request.GetResponse();
//获取服务端返回数据
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
result = sr.ReadToEnd().Trim();
sr.Close();
}
catch (System.Threading.ThreadAbortException e)
{
//Log.Error("HttpService", "Thread - caught ThreadAbortException - resetting.");
System.Threading.Thread.ResetAbort();
}
catch (WebException e)
{
//Log.Error("HttpService", e.ToString());
//if (e.Status == WebExceptionStatus.ProtocolError)
//{
//Log.Error("HttpService", "StatusCode : " + ((HttpWebResponse)e.Response).StatusCode);
//Log.Error("HttpService", "StatusDescription : " + ((HttpWebResponse)e.Response).StatusDescription);
//}
//throw new WxPayException(e.ToString());
}
catch (Exception e)
{
//Log.Error("HttpService", e.ToString());
//throw new WxPayException(e.ToString());
}
finally
{
//关闭连接和流
if (response != null)
{
response.Close();
}
if (request != null)
{
request.Abort();
}
}
return result;
}
更多推荐
已为社区贡献1条内容
所有评论(0)