码迷,mamicode.com
首页 > Windows程序 > 详细

HttpWebRequest调用API

时间:2020-03-16 16:27:02      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:obj   获取   cat   length   对象   serialize   tst   get   需要   

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

标签:obj   获取   cat   length   对象   serialize   tst   get   需要   

原文地址:https://www.cnblogs.com/jxl123456/p/12504563.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!