HttpMethod.Get -- System.Net.ProtocolViolationException: 'Cannot send a content-body with this verb-type.'

439 views Asked by At

I need to send XML request to REST API , it is Get Request (i cannot use Post). This code is working from Postman but in C# .Net 4.8.1 framework this code is not working , i am getting error 'Cannot send a content-body with this verb-type.'

I google a lot but getting answer , lot of people have same issue.

What wrong am I doing ? Thanks. Here is Code.

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

There are 1 answers

0
Lucky Star On

The above code is not working with .net 4.8.1.

I copy the same code in .Net 7.0 and it is working.

So I will be writing REST API in .Net 7.0 and calling this API from .Net 4.8

Thanks.