HttpWebRequest调用API
Posted 三千世界
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HttpWebRequest调用API相关的知识,希望对你有一定的参考价值。
public void HttpWebRequestPost()
{
string responseContent = string.Empty; var userNameAndPwd = new { userNameOrEmailAddress = "admin", password = "admin"}; var jsonTest = JsonConvert.SerializeObject(userNameAndPwd);//序列化传输的对象 byte[] byteArray = Encoding.UTF8.GetBytes(jsonTest); HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(abpTokenUrl);//需要请求的网址 request.Method = "POST"; request.ContentType = "application/json";//头部(文件一定要写) request.ContentLength = byteArray.Length; using (Stream reqStream = request.GetRequestStream()) { reqStream.Write(byteArray, 0, byteArray.Length); } using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) //获取request的返回结果 { using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default)) //读取返回流 { responseContent = sr.ReadToEnd().ToString(); var jsonResult = JsonConvert.DeserializeObject(responseContent); } }
}
以上是关于HttpWebRequest调用API的主要内容,如果未能解决你的问题,请参考以下文章
onActivityResult 未在 Android API 23 的片段上调用
HttpWebRequest.GetResponse() 不断超时
[AX2012中使用HttpWebRequest的POST XML文件