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( contentBod y).ToStrin g();
}
if (this.httpURLConn.Method.E quals("POS T"))
{
try
{
System.IO.StreamWriter stOut = new System.IO.StreamWriter(htt pURLConn.G etRequestS tream(), System.Text.Encoding.Defau lt);
stOut.Write(contentBody);
stOut.Close();
buffer = new System.IO.StreamReader(new System.IO.StreamReader(htt pURLConn.G etResponse ().GetResp onseStream (), System.Text.Encoding.Defau lt).BaseSt ream, new System.IO.StreamReader(htt pURLConn.G etResponse ().GetResp onseStream (), System.Text.Encoding.Defau lt).Curren tEncoding) ;
while ((bytesRead = buffer.Read(chunk, 0, chunk.Length)) != 0)
{
responseBody.Append(chunk, 0, bytesRead);
}
HttpWebResponse respCode = (HttpWebResponse)httpURLCo nn.GetResp onse();
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.
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(
}
if (this.httpURLConn.Method.E
{
try
{
System.IO.StreamWriter stOut = new System.IO.StreamWriter(htt
stOut.Write(contentBody);
stOut.Close();
buffer = new System.IO.StreamReader(new
while ((bytesRead = buffer.Read(chunk, 0, chunk.Length)) != 0)
{
responseBody.Append(chunk,
}
HttpWebResponse respCode = (HttpWebResponse)httpURLCo
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
ASKER
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.
Thanks for the help.
I solved that issue.
Now to get it working with SSL and sucessfully convert / compile to VB.
Thanks again.
H.
ASKER
Probably a dumb question, but is the "contentType" required?
I'm in the process of comparing my code to your example.
H.