首页 > Web开发 > 详细

HttpWebRequest发http参数

时间:2018-03-12 00:50:52      阅读:258      评论:0      收藏:0      [点我收藏+]

使用js发请求时,一般使用表单.json对象或者字符串

$.post(url,jsonStr)

服务端获取参数

Request.QueryString.Get();// GET参数

Request.Form.Get();// POST参数

由于一直是使用JS发请求,未注意过服务端收不到参数的情况

使用C#的HttpWebRequest发http请求时,却发现服务端收不到参数.(出现在使用POST方式时)

于是使用最这个办法,读取InputStream,可以拿到参数

byte[] byts = new byte[this.Request.InputStream.Length];
Request.InputStream.Read(byts, 0, byts.Length);
json = System.Text.Encoding.Default.GetString(byts);

如果要让服务端拿到POST的表单参数 Request.Form.Get(),那么传参数时如下

string postPara="id=1&name=xx";// 和 url上参数形式一样

byte[] data = System.Text.Encoding.UTF8.GetBytes(postPara);

request.ContentLength = data.Length;

reqStream = request.GetRequestStream();
reqStream.Write(data, 0, data.Length);
reqStream.Close();

 

HttpWebRequest发http参数

原文:https://www.cnblogs.com/mirrortom/p/8547075.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!