By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,795 Members | 1,216 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,795 IT Pros & Developers. It's quick & easy.

HttpWebRequest GetResponse 500 Error

P: n/a
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 <DESCRIPTIONSHORTis 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
}
Aug 17 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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 <DESCRIPTIONSHORTis 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
}

Aug 18 '08 #2

P: n/a

"Jim Owen" <ji**@online.nospamwrote in message
news:70**********************************@microsof t.com...
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
The Web server should have dumped the error in its log with more information
as to the cause of the error.

Aug 18 '08 #3

P: n/a
"Jim Owen" <ji**@online.nospamwrote in message
news:0C**********************************@microsof t.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

Aug 18 '08 #4

P: n/a
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" <ji**@online.nospamwrote in message
news:0C**********************************@microsof t.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

Aug 19 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.