HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(url); webReq.Method = "POST"; webReq.ContentType = "application/x-www-form-urlencoded"; byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(postData); webReq.ContentLength = bytes.Length; //加上这个后很快 不加这个响应很慢 webReq.Proxy = null; Stream reqStream = webReq.GetRequestStream(); reqStream.Write(bytes, 0, bytes.Length); reqStream.Close(); HttpWebResponse responseStream = (HttpWebResponse)webReq.GetResponse(); StreamReader webStream = new StreamReader(responseStream.GetResponseStream(), Encoding.Default); //网页返回信息 string message = webStream.ReadToEnd(); return message;
源码如上 ,提交到对方URL 那边跟踪的提交参数都是空的 我这边调试的时候 传过去的都有值得。求解
1、确定那边是POST接收参数噻
2、确定你传递了数据。写日志,跟踪
日志跟踪到本地传值的时候是有值的,跟对方对接的时候对方获取请求日志那 是没有值的,纠结。对方的接口是Post传值的 ,用get试了一下不行,接口文档上也写的是POST方式,关键是 有的可以有的到那边就成空的了
@zklveyjx:
webReq.ContentType = "application/x-www-form-urlencoded";
这个属性,添加到Header里面用。
webReq.Headers.Add("","");
吧GB2312换成UTF8试试
事实证明跟这个没有关系。。。。
参考下
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(string.Format(format_UriString, streamServerIp, streamServerPort));
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
string json = string.Format("{{\"ip\":\"{0}\", \"id\":\"{1}\"}}", ip, deviceid);
var httpContent = new StringContent(json, Encoding.UTF8);
var response = client.PostAsync(url, httpContent);
var responseString = response.Result.Content.ReadAsStringAsync();
}
reqStream.Close(); 这句去掉。
StreamReader webStream = new StreamReader(responseStream.GetResponseStream(), Encoding.Default);
这个编码方式改成跟上面的GB2312一样。