【发布时间】:2016-11-13 23:22:12
【问题描述】:
我需要向请求正文中需要 JSON 的服务发送 GET 请求。我知道 GET 请求不应该以这种方式使用,但我无法控制服务并且需要使用现有的 API,不管它可能被破坏了。
所以,这就是不起作用的:
var req = (HttpWebRequest)WebRequest.Create("localhost:3456");
req.ContentType = "application/json";
req.Method = "GET";
using (var w = new StreamWriter(req.GetRequestStream()))
w.Write(JsonConvert.SerializeObject(new { a = 1 }));
它失败了:
Unhandled Exception: System.Net.ProtocolViolationException: Cannot send a content-body with this verb-type.
at System.Net.HttpWebRequest.CheckProtocol(Boolean onRequestStream)
at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
at System.Net.HttpWebRequest.GetRequestStream()
有道理。如何绕过这个?
谢谢!
【问题讨论】: