Knowledge Base Articles » KB100233: Preventing HTTP 400 Bad Request Errors.

When requesting a page in Netscape whose URL contains spaces in a querystring, IIS will return an HTTP 400 'Bad request' error. Take the following example:

Response.Redirect "URLEncodeTest.asp?Name=The Guru"

If we compare the URL in the browser's address bar after attempting redirection, we will see a difference in how Netscape renders it compared to Internet Explorer:

Internet Explorer - http://bfserver1/DGTest/URLEncodeTest.asp?Name=The%20Guru
Netscape - http://bfserver1/DGTest/URLEncodeTest.asp?Name=The Guru


Notice how Internet Explorer has replaced the space in the querystring value with '%20' while the URL in Netscape still has the space, which will cause IIS to return an HTTP 400 'Bad request' error with the following message:

'Due to malformed syntax, the request could not be understood by the server. The client should not repeat the request without modifications.'

The obvious solution is to use Server.URLEncode which applies URL encoding rules to a string of ASCII characters. Applying URLEncoding to the entire URL, however, will still produce an error, but this time it will be an HTTP 404 'Page not found' error, and this applies to both browsers. So, in this example:

strURL = Server.URLEncode("URLEncodeTest.asp?Name=The Guru")
Response.Redirect(strURL)


we will see the following URL in the location bar of both browsers after attempting redirection:.

http://bfserver1/DGTest/URLEncodeTest%2Easp%3FName%3DThe+Guru

Therefore, the answer is just to URLEncode the querystring value that contains the spaces, as in the following error-free, cross-browser compatible example:

strValue = Server.URLEncode("The Guru")
Response.Redirect "URLEncodeTest.asp?Name=" & strValue


This produces the URL below in the address bar of both browsers:

http://bfserver1/DGTest/URLEncodeTest.asp?Name=The+Guru