Re: Post XML to a Java servlet using HttpWebRequest

From: Joerg Jooss (joerg.jooss_at_gmx.net)
Date: 08/26/04


Date: Thu, 26 Aug 2004 08:01:28 +0200

Kevin Schneider wrote:
> 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,

-- 
Joerg Jooss
joerg.jooss@gmx.net


Relevant Pages

  • XML Parse Problem
    ... SQL Server database. ... The kicker is that it only does this when submitted to the Java servlet ... JBuilder IDE locally the parse error is not ... BEFORE I issue the parse command so the XML is only logged when the error ...
    (comp.lang.java.programmer)
  • XML Parse Problem
    ... SQL Server database. ... The kicker is that it only does this when submitted to the Java servlet ... JBuilder IDE locally the parse error is not ... BEFORE I issue the parse command so the XML is only logged when the error ...
    (comp.lang.java.help)
  • Re: Stream.Write / Encoding problem
    ... I would test converting xml string to base64 and sending that then ... request and you at least has a starting point. ... > request stream and write my XML string to it. ...
    (microsoft.public.dotnet.framework)
  • RE: XML as a WebService parameter
    ... \par From your description, you have an ASP.NET webservice which has a webmethod accepting a XML string parameter, and you'll get validation error when you call the webservice, correct? ... \par Generally, for XML webserivice, since its underlying message is encoded via SOAP XML, therefore, we should not directly pass parameter or return value of raw XML string content. ... \par Microsoft MSDN Online Support Lead ...
    (microsoft.public.dotnet.framework.webservices)
  • WriteXml, DateTime, and Timezone data
    ... I am then passing this string back to a PowerBuilder module that is taking this XML string and doing what it needs to do with it. ... the TimeZone portion? ...
    (microsoft.public.dotnet.xml)