ASP.NET后台调用API方法

/// <summary>
/// 调用API POST请求与获取结果
/// </summary>

///URL 与 JSON串
public static string HttpPost(string Url, string postDataStr)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST";
request.ContentType = "application/json";
//request.ContentLength = postDataStr.Length;
StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.UTF8);
writer.Write(postDataStr);
writer.Flush();
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));
string retString = reader.ReadToEnd();

return retString;
}

上一篇:基于 HTML5 WebGL + WebVR 的 3D 虚实现实可视化系统


下一篇:获取ip ,百度地图坐标点 和 在 后台调用 url()