|
|
|
date: Sun, 17 Aug 2008 10:42:03 -0700,
group: microsoft.public.dotnet.framework.webservices
back
HttpWebRequest GetResponse 500 Error
Hi,
I've run into a set of errors I don't understand coming back from
HttpWebRequest.GetResponse, In one case, null is returned from the request
without an Exception and in the other the request does not appear to leave my
system yet still returns the 500 error.
In the code below, there is a xml-snippet that I use as a test. When I
run the test using the snippet, the server on the other side logs and
processes the request correctly without error.
But, if the value in the element <DESCRIPTIONSHORT> is changed so that
instead of 100 Cotton, it reads 100% Cotton, I get an error back from
GetResponse in the HttpWebResponse object that states the remote server
returned a 500 error. If I change the 100% to 100%%, GetResponse retuns a
null without an exception.
In working with another developer on the remote site, he sees and logs the
first request without error. But in both of the latter cases, the request
never reaches their server.
I'd appreciate any ideas as to what is happening. My searches on the net
have not located anything like this.
Thanks,
Jim
Here is the code:
string itemUpdXml = "&ID=" +
"<STOCKUPDATE>" +
" <SKU>" +
" <NAME>Test</NAME>" +
" <SKU>4359394342</SKU>" +
" <PRICE>24.9900</PRICE>" +
" <DESCRIPTIONSHORT>Men's Polo 100
cotton</DESCRIPTIONSHORT>" +
" <TAXABLE>Y</TAXABLE>" +
" </SKU>" +
"</STOCKUPDATE>";
HttpWebRequest webReq = null;
HttpWebResponse webResp = null;
try
{
webReq =
(HttpWebRequest)WebRequest.Create("http://host.somedomain.com/item.cfm");
webReq.AllowAutoRedirect = true;
webReq.AuthenticationLevel = System.Net.Security.AuthenticationLevel.None;
webReq.KeepAlive = true;
webReq.Method = "POST";
webReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0;
.NET CLR 1.1.4322)";
webReq.ContentType = "application/x-www-form-urlencoded";
webReq.ContentLength = itemUpdXml.Length;
webReq.AuthenticationLevel = System.Net.Security.AuthenticationLevel.None;
webReq.KeepAlive = true;
// Write the request
StreamWriter stOut = new StreamWriter(webReq.GetRequestStream(),
System.Text.Encoding.ASCII);
stOut.Write(itemUpdXml);
stOut.Close();
webResp = (HttpWebResponse)webReq.GetResponse();
}
catch (Exception ex)
{
// Do some error processing
}
date: Sun, 17 Aug 2008 10:42:03 -0700
author: Jim Owen am
RE: HttpWebRequest GetResponse 500 Error
All,
As a follow up to this - through the use of a packet sniffer, I was able to
determine that the request WAS going out to the remote server. The server
was, in fact, generating a 500 error, but long before the application code on
that side was able to execute. So the problem was not on our end, but the
remote end.
That leads to the question of what would cause the remote server to barf on
a single or double percent sign?? It's Cold Fusion on Win 2003.
Thanks, Jim
"Jim Owen" wrote:
> Hi,
> I've run into a set of errors I don't understand coming back from
> HttpWebRequest.GetResponse, In one case, null is returned from the request
> without an Exception and in the other the request does not appear to leave my
> system yet still returns the 500 error.
>
> In the code below, there is a xml-snippet that I use as a test. When I
> run the test using the snippet, the server on the other side logs and
> processes the request correctly without error.
>
> But, if the value in the element <DESCRIPTIONSHORT> is changed so that
> instead of 100 Cotton, it reads 100% Cotton, I get an error back from
> GetResponse in the HttpWebResponse object that states the remote server
> returned a 500 error. If I change the 100% to 100%%, GetResponse retuns a
> null without an exception.
>
> In working with another developer on the remote site, he sees and logs the
> first request without error. But in both of the latter cases, the request
> never reaches their server.
>
> I'd appreciate any ideas as to what is happening. My searches on the net
> have not located anything like this.
>
> Thanks,
>
> Jim
>
>
> Here is the code:
>
> string itemUpdXml = "&ID=" +
> "<STOCKUPDATE>" +
> " <SKU>" +
> " <NAME>Test</NAME>" +
> " <SKU>4359394342</SKU>" +
> " <PRICE>24.9900</PRICE>" +
> " <DESCRIPTIONSHORT>Men's Polo 100
> cotton</DESCRIPTIONSHORT>" +
> " <TAXABLE>Y</TAXABLE>" +
> " </SKU>" +
> "</STOCKUPDATE>";
>
> HttpWebRequest webReq = null;
> HttpWebResponse webResp = null;
>
> try
> {
> webReq =
> (HttpWebRequest)WebRequest.Create("http://host.somedomain.com/item.cfm");
> webReq.AllowAutoRedirect = true;
> webReq.AuthenticationLevel = System.Net.Security.AuthenticationLevel.None;
> webReq.KeepAlive = true;
> webReq.Method = "POST";
> webReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0;
> .NET CLR 1.1.4322)";
> webReq.ContentType = "application/x-www-form-urlencoded";
> webReq.ContentLength = itemUpdXml.Length;
> webReq.AuthenticationLevel = System.Net.Security.AuthenticationLevel.None;
> webReq.KeepAlive = true;
>
> // Write the request
> StreamWriter stOut = new StreamWriter(webReq.GetRequestStream(),
> System.Text.Encoding.ASCII);
> stOut.Write(itemUpdXml);
> stOut.Close();
>
> webResp = (HttpWebResponse)webReq.GetResponse();
> }
> catch (Exception ex)
> {
> // Do some error processing
> }
>
>
date: Sun, 17 Aug 2008 22:12:04 -0700
author: Jim Owen am
Re: HttpWebRequest GetResponse 500 Error
Hi John,
The sample XML that is within the code is there simply and only as a
debugging test case - I needed something I could use to duplicate the problem
reliably.
That said, we're still running into the problem - there's been no luck on
the remote side. However, they suggested as well that I URL encode the xml
for testing, which I've tried but again without success.
Here's the updated test code:
HttpWebRequest webReq = null;
webReq = (HttpWebRequest)WebRequest.Create(URLStr);
webReq.Method = "POST";
ASCIIEncoding encoding = new ASCIIEncoding();
string dataStr = HttpUtility.UrlEncode(xmlStr);
webReq.ContentType = "application/x-www-form-urlencoded";
// Send the data.
byte[] data = Encoding.ASCII.GetBytes(dataStr);
webReq.ContentLength = data.Length;
Stream newStream = webReq.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
webResp = (HttpWebResponse)webReq.GetResponse();
Still receiving the 500 error from the remote Cold Fusion server.
Any help would be appreciated.
- Jim
"John Saunders" wrote:
> "Jim Owen" <jimo@online.nospam> wrote in message
> news:0C3FDC1C-59CC-43A5-9879-2B1648E1CAB2@microsoft.com...
> >...
> > Here is the code:
> >
> > string itemUpdXml = "&ID=" +
> > "<STOCKUPDATE>" +
> > " <SKU>" +
> > " <NAME>Test</NAME>" +
> > " <SKU>4359394342</SKU>" +
> > " <PRICE>24.9900</PRICE>" +
> > " <DESCRIPTIONSHORT>Men's Polo 100
> > cotton</DESCRIPTIONSHORT>" +
> > " <TAXABLE>Y</TAXABLE>" +
> > " </SKU>" +
> > "</STOCKUPDATE>";
>
> I strongly suggest that you never do this again. XML is not text and must
> not be constructed using string concatenation. What are you planning to do
> it the description field contains characters that are illegal in XML? Use an
> XmlWriter instead, or else construct your XML in an XmlDocument and then
> send the contents of that. Both of these mechanisms understand XML, and will
> properly encode characters that are not permitted.
>
> Your current issue is probably due to the fact that you didn't URL Encode
> your XML. Percent-sign is treated specially in a URL. Have you ever seen a
> URL containing, for instance, %20?
>
> --
> John Saunders | MVP - Connected System Developer
>
>
date: Mon, 18 Aug 2008 23:12:13 -0700
author: Jim Owen am
|
|