Post XML to a Java servlet using HttpWebRequest

K

Kevin Schneider

I am trying to POST an XML string to a Java servlet, but I am getting
the error "The underlying connection was closed: An unexpected error
occurred on a receive." when I try to get the response. Here's the
relevant code (some of the names have been changed to protect the
innocent):

string lcUrl = "http://10.10.10.10:8503/Servlet";
HttpWebRequest loHttp = (HttpWebRequest) WebRequest.Create(lcUrl);

loHttp.Method="POST";
byte [] lbPostBuffer =
System.Text.Encoding.GetEncoding(1252).GetBytes(xmlRequest);
loHttp.ContentType = "text/xml";
loHttp.ContentLength = lbPostBuffer.Length;

//Send the POST data
Stream loPostData = loHttp.GetRequestStream();
loPostData.Write(lbPostBuffer,0,lbPostBuffer.Length);
loPostData.Close();

//////////
//Exception happens here
HttpWebResponse loWebResponse = (HttpWebResponse)
loHttp.GetResponse();
///////

Encoding enc = System.Text.Encoding.GetEncoding(1252);

StreamReader loResponseStream = new
StreamReader(loWebResponse.GetResponseStream(),enc);

string lcHtml = loResponseStream.ReadToEnd();
loWebResponse.Close();
loResponseStream.Close();


The XML data looks is
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
<ContactRequestElementDO>
<ContactSearchDO>
<ProductInstID>BBCB7987</ProductInstID>
</ContactSearchDO>
</ContactRequestElementDO>"


Any ideas?
 
J

Joerg Jooss

Kevin said:
I am trying to POST an XML string to a Java servlet, but I am getting
the error "The underlying connection was closed: An unexpected error
occurred on a receive." when I try to get the response. Here's the
relevant code (some of the names have been changed to protect the
innocent):

string lcUrl = "http://10.10.10.10:8503/Servlet";
HttpWebRequest loHttp = (HttpWebRequest) WebRequest.Create(lcUrl);

loHttp.Method="POST";
byte [] lbPostBuffer =
System.Text.Encoding.GetEncoding(1252).GetBytes(xmlRequest);
loHttp.ContentType = "text/xml";
loHttp.ContentLength = lbPostBuffer.Length;

//Send the POST data
Stream loPostData = loHttp.GetRequestStream();
loPostData.Write(lbPostBuffer,0,lbPostBuffer.Length);
loPostData.Close();

//////////
//Exception happens here
HttpWebResponse loWebResponse = (HttpWebResponse)
loHttp.GetResponse();
///////

Encoding enc = System.Text.Encoding.GetEncoding(1252);

StreamReader loResponseStream = new
StreamReader(loWebResponse.GetResponseStream(),enc);

string lcHtml = loResponseStream.ReadToEnd();
loWebResponse.Close();
loResponseStream.Close();
The XML data looks is
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
<ContactRequestElementDO>
<ContactSearchDO>
<ProductInstID>BBCB7987</ProductInstID>
</ContactSearchDO>
</ContactRequestElementDO>"

If that's your typical payload, then using
System.Text.Encoding.GetEncoding(1252)
is wrong. This gets a Windows-1252 Encoding instance, but your XML obviously
uses UTF-8.

Unfortunately, that isn't likely to fix the problem, since your sample XML
contains only ASCII characters, and the HTTP code looks OK (adding some
using() statements wouldn't hurt, though). Is there an error on the
server-side? Does the servlet process the request?

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top