标签:定义 pre str 简单的 open 信息 you cat mvc
在c#中调用外部API或者自己系统的API都是很简单的,这里提供使用HttpWebRequest的方式访问外部API,自定义了头部Access_key.
若外部系统没有此安全验证,可以去掉。
public string OpenReadWithHttps(string URL, string strPostdata, string strEncoding)
{
Encoding encoding = Encoding.Default;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Method = "post";
request.Headers.Add("access_key", "your access_key");
request.Accept = "application/json";
request.ContentType = "application/json; charset=utf-8";
byte[] buffer = Encoding.ASCII.GetBytes(strPostdata);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
//System.Text.Encoding.GetEncoding(strEncoding)
{
return reader.ReadToEnd();
}
}
【C#MVC】 使用HttpWebRequest访问外部API,并返回json信息 (Access_Key)
标签:定义 pre str 简单的 open 信息 you cat mvc
原文地址:https://www.cnblogs.com/dennyw/p/14229338.html