locked
You must write ContentLength bytes to the request stream before calling [Begin]GetResponse. RRS feed

  • Question

  • Hi guys,

    I am having some very wired error. In one of my HttpWebRequest.GetResponse() I have the error in the title. However, I use the same idea for another method yet I get no error.

    Here is the code I get the error:

     

    HttpWebRequest SendInviteReq = WebRequest.Create(url8) as HttpWebRequest;
                SendInviteReq.Method = "POST";
                string postMessage = "{}";
    
                SendInviteReq.ContentType = "application/json;charset=utf-8";
                SendInviteReq.ContentLength = postMessage.Length;
    
                Stream inviteStream = SendInviteReq.GetRequestStream();
                UTF8Encoding encoder4 = new UTF8Encoding();
                byte[] bytes4 = encoder4.GetBytes(postMessage);
                inviteStream.Write(bytes4, 0, bytes4.Length);
    
                
    
                HttpWebResponse SendInviteRes = (HttpWebResponse)SendInviteReq.GetResponse();      <--- ERROR IS HERE AT .GetResponse();
     StreamReader SendInviteLog = new StreamReader(SendInviteRes.GetResponseStream());

    Here is code pretty much identical, but I DO NOT get the error.

     

     

     

    HttpWebRequest LogOutReq = WebRequest.Create(url4) as HttpWebRequest;
                LogOutReq.Method = "PUT";
                string postData1 = "{\"presenceState\":-1}";
    
                LogOutReq.ContentType = "application/json;charset=utf-8";
                LogOutReq.ContentLength = postData1.Length;
    
                Stream dataStream1 = LogOutReq.GetRequestStream();
                UTF8Encoding encoder1 = new UTF8Encoding();
                byte[] bytes1 = encoder1.GetBytes(postData1);
                dataStream1.Write(bytes1, 0, bytes1.Length);
    
                HttpWebResponse res = (HttpWebResponse)LogOutReq.GetResponse();
                StreamReader LogOffReader = new StreamReader(res.GetResponseStream());
    

    Why when I execute the first code I get the error, but not for the second code?

     

     

    Thank you



    • Edited by babacloanta Thursday, December 8, 2011 7:12 AM
    Thursday, December 8, 2011 6:52 AM

All replies

  • Hi,

    I have the same error, but haven't resolved.

    The error is raised in this method: 

    WebResponse response = request.GetResponse();

    Code is bellow:

    WebRequest request = WebRequest.Create(new Uri(url));
    request.Method = "POST";
    
    
    byte[] dataBuffer = System.Text.Encoding.UTF8.GetBytes(xml);
    request.ContentType = "text/xml";
    request.ContentLength = dataBuffer.Length;
    
    Stream stream = request.GetRequestStream();
    stream.Write(dataBuffer, 0, dataBuffer.Length);
    request.Timeout = 300000;
    
    WebResponse response = request.GetResponse();
    stream = response.GetResponseStream();

    Regards,

    JP.


    • Edited by Joao Prata Wednesday, October 3, 2012 11:02 AM
    Wednesday, October 3, 2012 11:01 AM
  • Try using "GET" instead of "POST".  Also set data lenght to zero.  The GetRequestStream will set the return length.  It doesn't like any other value but zero.
    Thursday, October 4, 2012 12:49 PM
  • It works for me here in a quick test. I'd close the request stream before calling GetResponse. So change to:

    using (Stream stream = request.GetRequestStream()) {
       stream.Write(dataBuffer, 0, dataBuffer.Length);
    }
    


    http://www.alanjmcf.me.uk/ Please follow-up in the newsgroup. If I help, please vote and/or mark the question answered. Available for contract programming.

    Saturday, October 6, 2012 11:30 AM
  • Hi,

    I have resolved my problem using the following link:

    http://msdn.microsoft.com/en-us/library/debx8sh9.aspx

    Thanks for the responses,

    JP.

    Monday, October 8, 2012 9:25 AM
  • Hi,

    Unfortunately my code is not working, i had make the implementation above by the msdn link, it resolved me that problem, but i had a proxy problem that now is resolved, and now i have the begin problem. I have implemented Joel solution but it gives me the following error: "Cannot send a content-body with this verb-type", and with Alan solution it gives me the following error: "The remote server returned an error: (500) Internal Server Error".

    If i use my code, it gives me this error: "You must write ContentLength bytes to the request stream before calling [Begin]GetResponse"

    My code:

    WebRequest request = WebRequest.Create(new Uri(webServiceAddress));
    request.Method = "POST";
    
    byte[] dataBuffer = System.Text.Encoding.UTF8.GetBytes(xmlIn);
    request.ContentType = "text/xml";
    request.ContentLength = dataBuffer.Length;
    
    Stream stream = request.GetRequestStream();
    stream.Write(dataBuffer, 0, dataBuffer.Length);
    request.Timeout = 300000;
    
    WebResponse response = request.GetResponse();
    stream = response.GetResponseStream();

    Thanks in for the responses.

    JP.


    • Edited by Joao Prata Tuesday, October 16, 2012 11:17 AM
    Tuesday, October 16, 2012 11:05 AM
  • Webrequest doesn't like POST.  Change to GET or remove statement.  I also think you will get an error setting the length and you should remove the statement.  I had both of these eerrors a few weeks ago and resolved it by removing the statements.  You can use POST for the GetResponse but not sure if it is necessary.

    jdweng

    Tuesday, October 16, 2012 11:56 AM
  • Hi,

    I have changed the request method to "GET" and i have removed, and it gives me the following error: "Cannot send a content-body with this verb-type", in this context i think its necessary to send with request method with POST.

    What statement are you suggesting to be removed?

    Other thing i have checked is that stream.Lenght and stream.Position are throwing the following exception: "System.NotSupportedException".

    Thanks for the response.

    JP.


    • Edited by Joao Prata Tuesday, October 16, 2012 2:00 PM
    • Proposed as answer by Joao Prata Tuesday, October 16, 2012 2:43 PM
    • Unproposed as answer by Joao Prata Tuesday, October 16, 2012 2:43 PM
    Tuesday, October 16, 2012 1:49 PM
  • Hi, 

    I found the answer, if i put "stream.Close()" after "stream.write(....)", this works fine, i didnt understand why this works with this code.

    Regards,

    JP.

    Tuesday, October 16, 2012 2:45 PM