• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 186
  • Last Modified:

Exception Message "You must write ContentLength bytes to the request stream before calling [Begin]GetResponse."

I am getting this exception message
Message = "You must write ContentLength bytes to the request stream before calling [Begin]GetResponse."

Here is my code (Partially)
GlobalOneResponse objCharge = new GlobalOneResponse();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Method = "POST";
request.ContentLength = xmlString.Length; 
request.ContentType = "text/xml";
                       
StreamWriter requestWriter = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.UTF8);
requestWriter.Write(xmlString);
WebResponse webResponse = request.GetResponse();
Stream webStream = webResponse.GetResponseStream();
StreamReader responseReader = new StreamReader(webStream);

responsedata = responseReader.ReadToEnd();                      
responsedata = responsedata.Replace(System.Environment.NewLine, null);

Open in new window


The bad line is highlighted here
SP
0
Larry Brister
Asked:
Larry Brister
  • 3
  • 2
2 Solutions
 
Ares KurkluSoftware EngineerCommented:
I think you may have to  close the stream, check the example below:


                              request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;      
                              request.Credentials = new NetworkCredential(FTPUserName, FTPPassword);
                              request.Proxy = null;
                              FtpWebResponse response = (FtpWebResponse) request.GetResponse();
                              long size = response.ContentLength;
                              Stream datastream = response.GetResponseStream();
                              StreamReader sr = new StreamReader(datastream);
                              string result = sr.ReadToEnd();
                              sr.Close();
                              datastream.Close();                              
                              response.Close();
0
 
Larry Bristersr. DeveloperAuthor Commented:
Can you modify my code above or tell me what to pt where?
0
 
Ares KurkluSoftware EngineerCommented:
responsedata would be a string i suppose, declared somewhere up, just after you read it try to close all then manipulate the string.

            responsedata = responseReader.ReadToEnd();
            responseReader.Close();
            webStream.Close();
            webResponse.Close();
       
            responsedata = responsedata.Replace(System.Environment.NewLine, null);

Open in new window

0
 
Larry Bristersr. DeveloperAuthor Commented:
Hey guys...
This was the final solution
I will split points... you put me on the right track

request.Method = WebRequestMethods.Http.Post;
request.ContentType = "text/xml";
request.ContentLength = xmlString.Length;

using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
{
    writer.Write(xmlString);
}

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
    responsedata = reader.ReadToEnd();
}

Open in new window

0
 
Larry Bristersr. DeveloperAuthor Commented:
Gave me a start in my thought process
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now