放到服务器上就会这样,求助
System.Net.ProtocolViolation Exception: Content-Length or Chunked Encoding cannot be set for an operation that does not write data.
at System.Net.HttpWebRequest.CheckProtocol(Boolean onRequestStream)
at System.Net.HttpWebRequest.GetResponse()
—- 25分
Content-Length怎么设的? 是不是post数据有中文,计算的Content-Length不对
—-
post数据中没有中文 长度是根据传进来的参数设置的
//} /// <summary> /// Post 提交调用 /// </summary> /// <param name="url">提交地址</param> /// <param name="param">参数</param> /// <param name="Timeout">超时时间</param> /// <returns>string</returns> public string webRequestPost(string url, string param, int Timeout,System.Text.Encoding code) { byte[] bs = System.Text.Encoding.Default.GetBytes(param); HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url); req.Method = "post"; req.Timeout = Timeout * 1000; req.ContentType = "application/x-www-form-urlencoded;charset=utf-8"; req.ContentLength = bs.Length; using (Stream reqStream = req.GetRequestStream()) { reqStream.Write(bs, 0, bs.Length); reqStream.Flush(); } using (WebResponse wr = req.GetResponse()) { //在这里对接收到的页面内容进行处理 Stream strm = wr.GetResponseStream(); StreamReader sr = new StreamReader(strm, code); string line; System.Text.StringBuilder sb = new System.Text.StringBuilder(); while ((line = sr.ReadLine()) != null) { sb.Append(line + System.Environment.NewLine); } sr.Close(); strm.Close(); return sb.ToString(); } }
—-
我是接受一个请求调用在我这边处理的时候又重新请求了另外一个地址的接口的时候报的这个异常
—- 20分
看了两个帖子,都说和redirect相关的, 你看看
http://stackoverflow.com/questions/6636009/why-does-this-exception-claim-the-code-doesnt-write-data
http://forums.asp.net/t/1698399.aspx?Content+Length+or+Chunked+Encoding+cannot+be+set+for+an+operation+that+does+not+write+data+
—-
其中一篇说我请求的数据在重定向中丢失了! 但是我已经不需要之前的参数了啊?我之后请求的是我自己重新添加的参数啊?
—- 5分
你说本地没问题,那只有把post的url,data写log查看
—-
WebResponse?wr?=?req.GetResponse()前是没有问题的 ,参数也正常
flightdate=2014-11-23&depcity=CAN&aircity=PVG&airco=&triptype=1
flightdate=2014-11-23&depcity=CAN&aircity=PVG&airco=&triptype=1
—-
把post改成get方式就不会报错了!
CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明HttpWebRequest在post请求异常 ProtocolViolation!