提问人:Lucky Star 提问时间:4/14/2023 最后编辑:Lucky Star 更新时间:4/15/2023 访问量:257
HttpMethod.Get -- System.Net.ProtocolViolationException:“无法发送具有此谓词类型的内容正文。[复制]
HttpMethod.Get -- System.Net.ProtocolViolationException: 'Cannot send a content-body with this verb-type.' [duplicate]
问:
我需要向REST API发送XML请求,它是Get Request(我不能使用Post)。此代码可从 Postman 工作,但在 C# .Net 4.8.1 框架中,此代码不起作用,我收到错误“无法发送具有此动词类型的内容正文”。
我用谷歌搜索了很多,但得到了答案,很多人都有同样的问题。
我做错了什么?谢谢。 这是代码。
var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Get, "http://XXXX/get/getFees");
request.Headers.Add("Authorization", "Basic XXXX);
request.Headers.Add("Cookie", "JSESSIONID=XXXX");
var content = new StringContent("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\r\n <soap:Body>\r\n\r\n </soap:Body>\r\n</soap:Envelope>", null, "text/plain");
request.Content = content;
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
Console.WriteLine(await response.Content.ReadAsStringAsync());
答:
-1赞
Lucky Star
4/15/2023
#1
上面的代码不适用于 .net 4.8.1。
我在 .Net 7.0 中复制了相同的代码,它正在工作。
因此,我将在 .Net 7.0 中编写 REST API,并从 .Net 4.8 调用此 API
谢谢。
评论