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

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

Dont get the request stream, quite simply. GET requests dont usually have bodies (even though its not technically prohibited by HTTP) and WebRequest doesnt support it – but thats what calling GetRequestStream is for, providing body data for the request.

Given that youre trying to read from the stream, it looks to me like you actually want to get the response and read the response stream from that:

WebRequest request = WebRequest.Create(get.AbsoluteUri + args);
request.Method = GET;
using (WebResponse response = request.GetResponse())
{
    using (Stream stream = response.GetResponseStream())
    {
        XmlTextReader reader = new XmlTextReader(stream);
        ...
    }
}

I had the similar issue using Flurl.Http:

Flurl.Http.FlurlHttpException: Call failed. Cannot send a content-body with this verb-type. GET http://******:8301/api/v1/agents/**** ---> System.Net.ProtocolViolationException: Cannot send a content-body with this verb-type.

The problem was I used .WithHeader(Content-Type, application/json) when creating IFlurlRequest.

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

Because you didnt specify the Header.

Ive added an extended example:

var request = (HttpWebRequest)WebRequest.Create(strServer + strURL.Split(&)[1].ToString());

Header(ref request, p_Method);

And the method Header:

private void Header(ref HttpWebRequest p_request, string p_Method)
{
    p_request.ContentType = application/x-www-form-urlencoded;
    p_request.Method = p_Method;
    p_request.UserAgent = Mozilla/4.0 (compatible; MSIE 6.0; Windows CE);
    p_request.Host = strServer.Split(/)[2].ToString();
    p_request.Accept = */*;
    if (String.IsNullOrEmpty(strURLReferer))
    {
        p_request.Referer = strServer;
    }
    else
    {
        p_request.Referer = strURLReferer;
    }
    p_request.Headers.Add(Accept-Language, en-usrn);
    p_request.Headers.Add(UA-CPU, x86 rn);
    p_request.Headers.Add(Cache-Control, no-cachern);
    p_request.KeepAlive = true;
}

Leave a Reply

Your email address will not be published.