0
我需要发送一个GET请求给期望请求正文中的JSON的服务。我知道GET请求并不意味着以这种方式使用,但我无法控制该服务,并且需要使用现有的API,但是可能会破坏。如何使用C#发送包含JSON正文的GET请求?
所以,这里就是不工作:
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()
有道理。我如何绕过这个?
谢谢!