locked
ProtocolVioloationException during GetResponse of Chunked HttpWebRequest

    Question

  • Hi,

    I'm stumped with this one.  Here's the code

          byte[] contentBytes = System.Text.Encoding.ASCII.GetBytes(content);
          HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
          webRequest.Method = "POST";
          webRequest.SendChunked = true;
          webRequest.ContentLength = contentBytes.Length;
          System.IO.Stream requestStream = webRequest.GetRequestStream();
          requestStream.Write(contentBytes, 0, contentBytes.Length);
          requestStream.Close();
    
          HttpWebResponse webResponse = (HttpWebResponse) webRequest.GetResponse();
    

    I've tried closing the requestStream after the response but still the same exception when I GetResponse():

    ProtocolViolationException: Content-Length or Chunked Encoding cannot be set for an operation that does not write data.

    I'm clearly writing to the request stream, so what gives?

    Thanks In Advance,

    Steve

    Wednesday, August 18, 2010 2:48 AM

Answers

  • My bad, the server was sending a 303 Redirect so I would have to repost the data.

    Interesting nugget of info.

    S

    • Marked as answer by Steve Thiakos Thursday, August 19, 2010 7:35 PM
    Thursday, August 19, 2010 7:35 PM

All replies

  • My bad, the server was sending a 303 Redirect so I would have to repost the data.

    Interesting nugget of info.

    S

    • Marked as answer by Steve Thiakos Thursday, August 19, 2010 7:35 PM
    Thursday, August 19, 2010 7:35 PM
  • This doesnt make sense. From your code, it appears you are doing everything right, (although setting both chunked and content-length is not needed, you should set one or the other). This exception you are getting (ProtocolViolationException) is very confusing. If the redirect (303) does not require content to be posted again, then the system.net library should have drained your content transparently. It should not be throwing this exception.

    Can you get a repro with tracing enabled (see instructions in my signature) and post the trace log on pastebin.com and put a link here?


    feroze
    --
    My blog (including System.Net topics

     Subscribe in a reader

    Instruction on how to create a tracelog with your System.Net application
    System.Net Links and HOWTOs
    Monday, August 23, 2010 8:26 PM