none
HttpWebRequest ProtocolViolationException for GET with Request Data RRS feed

  • Question

  • Hi,

    When I try to use EventBrite API's in HttpWebRequest, GET request and try to add payload data in to Request.GetRequestStream() I receive ProtocolViolationException exception.

    Is there any flag or property that needs to be set?

    I did not find any Properties that will enable a GET method with Request Body.

    Request.Method = "GET"
    using (Stream Writer = Request.GetRequestStream())
                {
                    byte[] Data = Enc.GetBytes(RequestInfo.Data);
                    Writer.Write(Data, 0, Data.Length);
                }


    Thank you.

    Thursday, July 18, 2019 6:35 AM

All replies

  • Hi Harish,

    Thank you for posting here.

    Based on your description, you want to solve the exception when you add data into Request.GetRequestStream().

    I think you could try "POST" method instead of "GET" method. If you want to know more about it, you could look at the following link.

    https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebrequest.method?view=netframework-4.8

    Here is a simple code for your reference.

     HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("http://www.contoso.com/");
                Request.Method = "POST";
                string a = "hello, world";
                ASCIIEncoding encoding = new ASCIIEncoding();
                byte[] data = encoding.GetBytes(a);
                Request.ContentType = "application/x-www-form-urlencoded";
                Request.ContentLength = data.Length;
                using (Stream Writer = Request.GetRequestStream())
                {
                    Writer.Write(data, 0, data.Length);
                }
    

    If you want to use EventBrite API to do it, please provide the related code.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, July 18, 2019 7:38 AM
    Moderator
  • Hello Jack.

    The EventBrite API specifies that it has to be GET, So I will not be able to use POST call to consume it. What I am looking for is to get clarified if there is a way that .net Framework would allow me to send Request Data when method is set as GET. 

    I am on .net 4.7.1 framework.


    Thank you.

    Thursday, July 18, 2019 12:39 PM
  • You cannot send a body with a GET request. This is documented in the HTTP standard and referenced in almost every discussion on HTTP. More specifically the standard says that anything sent in the body has no semantic value and can be ignored. While a server implementation may allow it the issue comes in that the intermediate caches between the client and server (generally proxies) may or may not store the body (because the spec says it doesn't have one). Hence it might work if you're on premise but not remotely.

    GETs only have a header. All data for a GET must be passed in the URL. If a request require a body then it cannot be a GET request.

    You didn't mention the actual API you're calling but I suspect if you look at their docs it isn't a GET or it doesn't have a request body. However since that is a third party API you'll need to post that question in their forums.


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, July 18, 2019 1:42 PM
    Moderator
  • Yes, I am aware of the HTTP documentation and also read the documentation history on how it has been updated vaguely over years to allow request body in GET. Also I am aware that the 3rd party may or may not adhere to HTTP documentations.

    What caught me is, someone told me that this is possible in .net core so you should try and check why .net framework doesn't support. I am confused and trying to be sure that there isn't any such ways which I am not aware off.

    Thursday, July 18, 2019 4:29 PM
  • It's not a client-side thing. GET requests don't have bodies so ANY proxy between you and the remote server can drop any body you send. Whether your client implementation (.NET Framework, .NET Core, Ruby, Node.js, etc) simply forwards any attached body on for a GET request is irrelevant. Proxies and/or server implementations can ignore it as per the spec. 

    https://stackoverflow.com/questions/978061/http-get-with-request-body

    https://stackoverflow.com/questions/5216567/is-this-statement-correct-http-get-method-always-has-no-message-body

    https://evertpot.com/dropbox-post-api/

    So, yes you can send a body with GET (because you can send whatever you want), yes it may work if you are within the same network. No it is not reliable as anything in between you and the remote server can ignore it. So why you would ever try to use something that is clearly not supported doesn't make sense to me. I'm still struggling to figure out which endpoint you're trying to call that has a body for the GET request and whether you've contacted the API devs to ask them about it?


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, July 18, 2019 8:15 PM
    Moderator