HttpWebRequest调用API
2021-03-14 06:32
标签:obj 获取 cat length 对象 serialize tst get 需要 HttpWebRequest调用API 标签:obj 获取 cat length 对象 serialize tst get 需要 原文地址:https://www.cnblogs.com/jxl123456/p/12504563.htmlpublic 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);
}
}
}
上一篇:C#下实现动态系统托盘图标
下一篇:C# 有道API翻译
文章标题:HttpWebRequest调用API
文章链接:http://soscw.com/index.php/essay/64457.html