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
Larry Bristersr. DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.