Share via


Is it possible to send content body with get request in c# using HTTP 1.1

Question

Wednesday, March 5, 2014 3:39 PM

Is it possible to send content body with get request in c# using HTTP 1.1(RFC 2616). I also Need How to implement this in c#. I am getting protocol violation exception:can't send content body with this verb type.

All replies (4)

Wednesday, March 5, 2014 4:57 PM ✅Answered | 1 vote

Hi,

take a look at this discussion:

http://stackoverflow.com/questions/3981564/cannot-send-a-content-body-with-this-verb-type

Although it is not prohibited to send a body with a get-request it is not recommended!

Kind regards,
Thomas Fröhle
App-Entwickler-Hotline für MSDN Online Deutschland

Disclaimer:
Bitte haben Sie Verständnis dafür, dass wir hier auf Rückfragen gar nicht oder nur sehr zeitverzögert antworten können.
Bitte nutzen Sie für Rückfragen oder neue Fragen den telefonischen Weg über die App-Entwickler-Hotline: http://www.msdn-online.de/Hotline

Es gelten für die App-Entwickler-Hotline und dieses Posting diese Nutzungsbedingungen, Hinweise zu MarkenzeichenInformationen zur Datensicherheit sowie die gesonderten Nutzungsbedingungen für die App-Entwickler-Hotline.


Friday, March 14, 2014 6:42 AM ✅Answered

Hi Dineshkumar,

The get request has no request body, why you want to send content body with it? http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.3. Maybe what you need is to rethink requirement.

Regards,

We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.


Wednesday, March 5, 2014 5:12 PM

Thanks thomas. But i need to implement this in my c# code using Http 1.1(RFC 2616). Is it possible using any nuget packages or third party tools.


Saturday, November 24, 2018 1:54 PM

getting following error msg:**
{"Cannot send a content-body with this verb-type."}**

public TResponse GetData<TResponse, TRequest>(string partialUrl, TRequest request,
            IEnumerable<KeyValuePair<string, IEnumerable<string>>> headers = null, bool useGZip = false)
        {
            string jsonString;
            var url = _baseUrl != null ? _baseUrl + partialUrl : partialUrl;
            WebRequest req = WebRequest.Create(url);
            req.Method = "GET";

            //client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            if (useGZip)
            {
                req.Headers.Add("AcceptEncoding", "gzip");
            }

            SetHeadersWebRequest(headers, req);

            var jsonRequest = JsonConvert.SerializeObject(request);
            /*var content = new StringContent(jsonRequest, Encoding.UTF8, "application/json");
            var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, partialUrl);
            httpRequestMessage.Content = content;*/
            var sendBytes = Encoding.UTF8.GetBytes(jsonRequest);
            var requestStream = req.GetRequestStream();
            requestStream.Write(sendBytes, 0, sendBytes.Length);
            requestStream.Close();

            using (HttpWebResponse response = (HttpWebResponse) req.GetResponse())
            {


                if (response.Headers.GetValues("Content-Encoding").Any(x => x == "gzip"))
                {
                    using (Stream stream = response.GetResponseStream())
                    using (Stream decompressed = new GZipStream(stream, CompressionMode.Decompress))
                    using (StreamReader reader = new StreamReader(decompressed))
                    {
                        jsonString = reader.ReadToEnd();
                    }
                }
                else
                {
                    using (var stream = response.GetResponseStream())
                    using (StreamReader sr = new StreamReader(stream))
                    {
                        jsonString = sr.ReadToEnd();
                    }
                }
            }
            return JsonConvert.DeserializeObject<TResponse>(jsonString);
        }