Link to home
Start Free TrialLog in
Avatar of Homer2040
Homer2040

asked on

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

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.
ASKER CERTIFIED SOLUTION
Avatar of whatsit2002
whatsit2002
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Homer2040
Homer2040

ASKER

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.
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.