.net发送HTTP请求

 1 string postData = "appKey=" + appKey;//参数
 2 postData += ("&appSecret=" + appSecret);
 3 
 4 var request = (HttpWebRequest)WebRequest.Create(url);
 5 request.Method = "POST";//请求方式
 6 request.ContentType = "application/x-www-form-urlencoded";//请求内容类型
 7 var data = Encoding.UTF8.GetBytes(postData);//获取长度
 8  request.ContentLength = data.Length;
 9 Stream newStream = request.GetRequestStream();
10 
11  //send
12 newStream.Write(data, 0, data.Length);
13 newStream.Close();
14 
15 //get
16 WebResponse response = request.GetResponse();
17 newStream = response.GetResponseStream();
18 StreamReader reader = new StreamReader(newStream);
19 string responseFromServer = reader.ReadToEnd();//得到返回数据
20 reader.Close();
21 response.Close();

 

posted @ 2020-06-20 17:23  清风徐来i  阅读(970)  评论(0编辑  收藏  举报