HttpWebRequest调用API

2021-03-14 06:32

阅读:392

标签: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


评论


亲,登录后才可以留言!