Re: Post XML to a Java servlet using HttpWebRequest
From: Joerg Jooss (joerg.jooss_at_gmx.net)
Date: 08/26/04
- Next message: Joerg Jooss: "Re: Showing up webpages/HTML in C# Program"
- Previous message: Jon Skeet [C# MVP]: "Re: New .NET website"
- In reply to: Kevin Schneider: "Post XML to a Java servlet using HttpWebRequest"
- Messages sorted by: [ date ] [ thread ]
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
- Next message: Joerg Jooss: "Re: Showing up webpages/HTML in C# Program"
- Previous message: Jon Skeet [C# MVP]: "Re: New .NET website"
- In reply to: Kevin Schneider: "Post XML to a Java servlet using HttpWebRequest"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|