We help IT Professionals succeed at work.

C# .NET client app not writing to stream for posting to website

Homer2040
Homer2040 asked
on
544 Views
Last Modified: 2013-11-05
I converted a program from java to c# using the JLCA in VS 2005.
I am trying to POST a string of values to a website and read the response returned.
It appears I am having a problem actually writing the string to the stream.
Here is the code:
----------------------------------------------------------
public virtual HttpResponse sendMessage()
{
      System.Text.StringBuilder responseBody = new System.Text.StringBuilder();
      HttpResponse httpResponse = new HttpResponse();
      System.IO.StreamReader buffer;
      char[] chunk = new char[2048];
      int bytesRead = 0;
      if (contentBody == null)
      {
            this.lastContentBody = "";
      }
      else
      {
                      this.lastContentBody = new System.Text.StringBuilder(contentBody).ToString();
      }

                      if (this.httpURLConn.Method.Equals("POST"))
      {

      try
      {
            System.IO.StreamWriter stOut = new System.IO.StreamWriter(httpURLConn.GetRequestStream(), System.Text.Encoding.Default);
            stOut.Write(contentBody);
            stOut.Close();

            buffer = new System.IO.StreamReader(new System.IO.StreamReader(httpURLConn.GetResponse().GetResponseStream(), System.Text.Encoding.Default).BaseStream, new System.IO.StreamReader(httpURLConn.GetResponse().GetResponseStream(), System.Text.Encoding.Default).CurrentEncoding);

            while ((bytesRead = buffer.Read(chunk, 0, chunk.Length)) != 0)
            {
                  responseBody.Append(chunk, 0, bytesRead);
            }

            HttpWebResponse respCode = (HttpWebResponse)httpURLConn.GetResponse();
            httpResponse.ResponseCode = (int)respCode.StatusCode;
      }

      catch (System.IO.IOException e)
      {
            //throw exception
      }
----------------------------------------------------------

Using VS debugging tools, I can see the "contentBody" string is a name/value set and is formatted correctly.
I receive and read the response correctly (verified by the server logs).

However, the reponse I am receiving is an error that indicates the string "contentBody"
is not getting written to the stream.

Any help is appreciated.

Thanks.

H.
Comment
Watch Question

This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
whatsit2002:

Probably a dumb question, but is the "contentType" required?

I'm in the process of comparing my code to your example.

H.
Yes, it is a good idea to set the media type you will be sending, however, I do not believe it is required to use the class.

Author

Commented:
whatsit2002:
Thanks for the help.
I solved that issue.

Now to get it working with SSL and sucessfully convert / compile to VB.

Thanks again.

H.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.