0

I try to GET data using HttpWebRequest.
But this line of code: Stream requestStream = request.GetRequestStream() is giving me this error:

Cannot send a content-body with this verb-type.

How can I fix this?

var data = new
{
   COMTAXCODE = "abcd"
};
var httpWebRequest = (HttpWebRequest)WebRequest.Create(endpoint);
httpWebRequest.Method = "GET";
httpWebRequest.Headers[HttpRequestHeader.Authorization] = token;
byte[] byteArray = Encoding.UTF8.GetBytes(JSONHelper.ObjectToJson(data));
httpWebRequest.ContentLength = byteArray.Length;
string res = "";
using (Stream requestBody = httpWebRequest.GetRequestStream())
{
   requestBody.Write(byteArray, 0, byteArray.Length);
}
using (HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
  res = reader.ReadToEnd();
}
Peter Csala
  • 4,906
  • 7
  • 11
  • 29
  • 3
    GET has no body. Period. No web page or service expects to find a body with a GET. Most proxies, firewalls and servers would reject such a request outright – Panagiotis Kanavos Jan 07 '21 at 10:43
  • 2
    Does this answer your question? [HTTP GET with request body](https://stackoverflow.com/questions/978061/http-get-with-request-body) – Peter Csala Jan 07 '21 at 10:45
  • What are you trying to do in the first place? And why use HttpWebRequest instead of HttpClient? In .NET Core and .NET (Core) 5, HttpWebRequest is actually implemented as a wrapper over HttpClient. You shouldn't need more than a few lines to make a GET with a specific token. – Panagiotis Kanavos Jan 07 '21 at 10:47

0 Answers0