c#怎么通过http post报文调用web接口
1个回答
推荐于2017-10-01 · 知道合伙人软件行家
关注
展开全部
string url = "http://127.0.0.1/page.aspx";
string postData = "id=1&user=hzt";
WebRequest request = WebRequest.Create(url);
request.Method = "Post";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postData.Length;
StreamWriter sw = new StreamWriter(request.GetRequestStream());
sw.Write(postData);
sw.Flush();
WebResponse response = request.GetResponse();
Stream s = response.GetResponseStream();
StreamReader sr = new StreamReader(s, Encoding.GetEncoding("gb2312"));
MessageBox.Show(sr.ReadToEnd());
sw.Dispose();
sw.Close();
sr.Dispose();
sr.Close();
s.Dispose();
s.Close();
string postData = "id=1&user=hzt";
WebRequest request = WebRequest.Create(url);
request.Method = "Post";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postData.Length;
StreamWriter sw = new StreamWriter(request.GetRequestStream());
sw.Write(postData);
sw.Flush();
WebResponse response = request.GetResponse();
Stream s = response.GetResponseStream();
StreamReader sr = new StreamReader(s, Encoding.GetEncoding("gb2312"));
MessageBox.Show(sr.ReadToEnd());
sw.Dispose();
sw.Close();
sr.Dispose();
sr.Close();
s.Dispose();
s.Close();
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询