次の方法で共有


HOWTO: Useful ASP page to return configurable HTTP Status codes

Sometimes, you just want a simple test page on IIS to do some automated testing of your code that interacts with IIS. Here is another useful one in the series.

Question:

Hi,

I have some negative testing therefore need to have IIS to return 403, 404 or 500. How can I configure each? An automated way would be the best but manual is OK too.

Thanks,

Answer:

There are a couple of approaches to do this. You can either:

  1. Configure IIS to go down the actual custom error codepath to generate responses with those status codes
  2. Make a request to a URL that generates responses with those status codes

The former can be accomplished in the following manner:

  1. There are many types of 403s. For example, you can configure IIS to:
    • Disallow Read permissions and then try to access a resource handled by the Static File Handler.
    • Disallow Dir Browsing and make sure no file resource matches DefaultDoc and then make a request to "/"
    • Etc... just check out the 403*.htm custom error pages for ideas
  2. 404 can be generated by making a request to /NotExist.htm
  3. 500 can be generated by making a request to an ASP page with a syntax error (for example, just remove the %> tag of an ASP page

The latter is easier to configure/use and is shown as a code sample at the end of this blog entry. You just need to copy the ASP page to IIS, make sure ASP is enabled, and make a request to that ASP page with the correct querystring parameter. i.e.

/ResponseStatus.asp?status=403%20Test!

//David

 <%
DIM status
status = Request.QueryString( "status" )

IF NOT IsEmpty( status ) THEN
    Response.Status = status
END IF
%>

Comments

  • Anonymous
    May 25, 2006
    The comment has been removed

  • Anonymous
    June 14, 2006
    Rich - the ASP code sends a HTTP response with the specified status code and no entity body. Thus, what you are observing is browser-dependent behavior.

    If ShowFriendly=1, IE chooses to show its hardcoded "friendly" HTML server error page, regardless of what the server sent.

    If ShowFriendly=0, IE parses the non-standard HTML body of "" sent by the server, and instead generates the more proper HTML text you copy/pasted and displays that.

    All of this is by-design.

    //David

  • Anonymous
    March 16, 2010
    try Send HTTP Tool to verify you HTTP status code.

  • Anonymous
    September 29, 2010
    Nice suggestion.  Here is a page that does almost exactly what you suggest.  It may be useful to people that want a quick and easy want to test HTTP error codes and status codes. savanttools.com/test-http-status-codes.asp

  • Anonymous
    June 29, 2012
    HOw can i set http redirect such as 403 for direct accessing images such in appache I am using .htaccess RewriteEngine on RewriteCond %{HTTP_REFERER} !^http://(www.)?localhost [NC] RewriteCond %{HTTP_REFERER} !^http://(www.)?localhost.*$ [NC] RewriteRule .(gif|jpg)$ - [F]