Tuesday, August 28, 2007

HTTP Status Codes Explained


HTTP, Hypertext Transfer Protocol, is the method by which clients (i.e. you) and servers communicate. When someone clicks a link, types in a URL or submits out a form, their browser sends a request to a server for information. It might be asking for a page, or sending data, but either way, that is called an HTTP Request. When a server receives that request, it sends back an HTTP Response, with information for the client. Usually, this is invisible, though I'm sure you've seen one of the very common Response codes - 404, indicating a page was not found. There are a fair few more status codes sent by servers, and the following is a list of the current ones in HTTP 1.1, along with an explanation of their meanings.

A more technical breakdown of HTTP 1.1 status codes and their meanings is available at http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html. There are several versions of HTTP, but currently HTTP 1.1 is the most widely used.

Informational

  • 100 - Continue
    A status code of 100 indicates that (usually the first) part of a request has been received without any problems, and that the rest of the request should now be sent.
  • 101 - Switching Protocols
    HTTP 1.1 is just one type of protocol for transferring data on the web, and a status code of 101 indicates that the server is changing to the protocol it defines in the "Upgrade" header it returns to the client. For example, when requesting a page, a browser might receive a statis code of 101, followed by an "Upgrade" header showing that the server is changing to a different version of HTTP.
Successful
  • 200 - OK
    The 200 status code is by far the most common returned. It means, simply, that the request was received and understood and is being processed.
  • 201 - Created
    A 201 status code indicates that a request was successful and as a result, a resource has been created (for example a new page).
  • 202 - Accepted
    The status code 202 indicates that server has received and understood the request, and that it has been accepted for processing, although it may not be processed immediately.
  • 203 - Non-Authoritative Information
    A 203 status code means that the request was received and understood, and that information sent back about the response is from a third party, rather than the original server. This is virtually identical in meaning to a 200 status code.
  • 204 - No Content
    The 204 status code means that the request was received and understood, but that there is no need to send any data back.
  • 205 - Reset Content
    The 205 status code is a request from the server to the client to reset the document from which the original request was sent. For example, if a user fills out a form, and submits it, a status code of 205 means the server is asking the browser to clear the form.
  • 206 - Partial Content
    A status code of 206 is a response to a request for part of a document. This is used by advanced caching tools, when a user agent requests only a small part of a page, and just that section is returned.
Redirection
  • 300 - Multiple Choices
    The 300 status code indicates that a resource has moved. The response will also include a list of locations from which the user agent can select the most appropriate.
  • 301 - Moved Permanently
    A status code of 301 tells a client that the resource they asked for has permanently moved to a new location. The response should also include this location. It tells the client to use the new URL the next time it wants to fetch the same resource.
  • 302 - Found
    A status code of 302 tells a client that the resource they asked for has temporarily moved to a new location. The response should also include this location. It tells the client that it should carry on using the same URL to access this resource.
  • 303 - See Other
    A 303 status code indicates that the response to the request can be found at the specified URL, and should be retrieved from there. It does not mean that something has moved - it is simply specifying the address at which the response to the request can be found.
  • 304 - Not Modified
    The 304 status code is sent in response to a request (for a document) that asked for the document only if it was newer than the one the client already had. Normally, when a document is cached, the date it was cached is stored. The next time the document is viewed, the client asks the server if the document has changed. If not, the client just reloads the document from the cache.
  • 305 - Use Proxy
    A 305 status code tells the client that the requested resource has to be reached through a proxy, which will be specified in the response.
  • 307 - Temporary Redirect
    307 is the status code that is sent when a document is temporarily available at a different URL, which is also returned. There is very little difference between a 302 status code and a 307 status code. 307 was created as another, less ambiguous, version of the 302 status code.
Client Error
  • 400 - Bad Request
    A status code of 400 indicates that the server did not understand the request due to bad syntax.
  • 401 - Unauthorized
    A 401 status code indicates that before a resource can be accessed, the client must be authorised by the server.
  • 402 - Payment Required
    The 402 status code is not currently in use, being listed as "reserved for future use".
  • 403 - Forbidden
    A 403 status code indicates that the client cannot access the requested resource. That might mean that the wrong username and password were sent in the request, or that the permissions on the server do not allow what was being asked.
  • 404 - Not Found
    The best known of them all, the 404 status code indicates that the requested resource was not found at the URL given, and the server has no idea how long for.
  • 405 - Method Not Allowed
    A 405 status code is returned when the client has tried to use a request method that the server does not allow. Request methods that are allowed should be sent with the response (common request methods are POST and GET).
  • 406 - Not Acceptable
    The 406 status code means that, although the server understood and processed the request, the response is of a form the client cannot understand. A client sends, as part of a request, headers indicating what types of data it can use, and a 406 error is returned when the response is of a type not i that list.
  • 407 - Proxy Authentication Required
    The 407 status code is very similar to the 401 status code, and means that the client must be authorised by the proxy before the request can proceed.
  • 408 - Request Timeout
    A 408 status code means that the client did not produce a request quickly enough. A server is set to only wait a certain amount of time for responses from clients, and a 408 status code indicates that time has passed.
  • 409 - Conflict
    A 409 status code indicates that the server was unable to complete the request, often because a file would need to be editted, created or deleted, and that file cannot be editted, created or deleted.
  • 410 - Gone
    A 410 status code is the 404's lesser known cousin. It indicates that a resource has permanently gone (a 404 status code gives no indication if a resource has gine permanently or temporarily), and no new address is known for it.
  • 411 - Length Required
    The 411 status code occurs when a server refuses to process a request because a content length was not specified.
  • 412 - Precondition Failed
    A 412 status code indicates that one of the conditions the request was made under has failed.
  • 413 - Request Entity Too Large
    The 413 status code indicates that the request was larger than the server is able to handle, either due to physical constraints or to settings. Usually, this occurs when a file is sent using the POST method from a form, and the file is larger than the maximum size allowed in the server settings.
  • 414 - Request-URI Too Long
    The 414 status code indicates the the URL requested by the client was longer than it can process.
  • 415 - Unsupported Media Type
    A 415 status code is returned by a server to indicate that part of the request was in an unsupported format.
  • 416 - Requested Range Not Satisfiable
    A 416 status code indicates that the server was unable to fulfill the request. This may be, for example, because the client asked for the 800th-900th bytes of a document, but the document was only 200 bytes long.
  • 417 - Expectation Failed
    The 417 status code means that the server was unable to properly complete the request. One of the headers sent to the server, the "Expect" header, indicated an expectation the server could not meet.
Server Error
  • 500 - Internal Server Error
    A 500 status code (all too often seen by Perl programmers) indicates that the server encountered something it didn't expect and was unable to complete the request.
  • 501 - Not Implemented
    The 501 status code indicates that the server does not support all that is needed for the request to be completed.
  • 502 - Bad Gateway
    A 502 status code indicates that a server, while acting as a proxy, received a response from a server further upstream that it judged invalid.
  • 503 - Service Unavailable
    A 503 status code is most often seen on extremely busy servers, and it indicates that the server was unable to complete the request due to a server overload.
  • 504 - Gateway Timeout
    A 504 status code is returned when a server acting as a proxy has waited too long for a response from a server further upstream.
  • 505 - HTTP Version Not Supported
    A 505 status code is returned when the HTTP version indicated in the request is no supported. The response should indicate which HTTP versions are supported.

JAVA CODE FOR HTTP CODE

public class HTTPCode
{
public static String GetHTTPStatusCodeDesc(int pintHTTPStatusCode ,boolean pbolVerbose ) As String
/*******************************************************************************
'
' Function : GetHTTPStatusCodeDesc
'
' Return Type : String
'
' Copyright : 2004 - Salty Brine Software (Art Araya)
'
' Description : Provides either a brief or verbose description for an
' HTTP Status Code.
'
' Parameters : 1) pintHTTPStatusCode (Integer) - the HTTP Status Code
' that a desciption Is needed For
' : 2) pbolVerbose (Boolean) - Used to specify whether a brief or
' verbose description of the status code Is desired.
'
' Range Description
' 100-199 Informational
' 200-299 Success: indicates that a client's request arrived and was accepted.
' 300-399 Client request redirection, further action required.
' 400-499 Client error
' 500-599 Server error
'
'*******************************************************************************/

String strHTTPStatusCodeDesc ="";

if (pbolVerbose )
{

switch( pintHTTPStatusCode)
{
Case 100:
strHTTPStatusCodeDesc = "Continue - The client can continue its request.";;;
Case 101:
strHTTPStatusCodeDesc = "Switching Protocols - The server has complied with the client's request to switch protocols.";
Case 200:
strHTTPStatusCodeDesc = "OK - The request was successful.";
Case 201:
strHTTPStatusCodeDesc = "Created - The request was successful and a new resource was created as a result.";
Case 202:
strHTTPStatusCodeDesc = "Accepted - The request was accepted for processing.";
Case 203:
strHTTPStatusCodeDesc = "Non-Authoritative Information - The meta-information in the entity-header is from a local or third-party copy.";
Case 204:
strHTTPStatusCodeDesc = "No Content - The server serviced the request but there is no information to return.";
Case 205:
strHTTPStatusCodeDesc = "Reset Content - The request was successful and the user agent should clear the input form for further input.";
Case 206:
strHTTPStatusCodeDesc = "Partial Content - The server serviced a (byte) range request for the resource.";
Case 300:
strHTTPStatusCodeDesc = "Multiple Choices - The requested resource could be one of multiple resources.";
Case 301:
strHTTPStatusCodeDesc = "Moved Permanently - The requested resource has permanently moved to a new location.";
Case 302:
strHTTPStatusCodeDesc = "Moved Temporarily - The requested resource has temporarily moved to a new location.";
Case 303:
strHTTPStatusCodeDesc = "See Other - The response to the request is under a different URI and can be retrieved with a GET request.";
Case 304:
strHTTPStatusCodeDesc = "Not Modified - The requested resource has not been modified since it was last requested.";
Case 305:
strHTTPStatusCodeDesc = "Use Proxy - The requested resource must be accessed through a proxy server.";
Case 400:
strHTTPStatusCodeDesc = "Bad Request - The server cannot read the request because its syntax is incorrect.";
Case 401:
strHTTPStatusCodeDesc = "Unauthorized - The server must authenticate the user before servicing the request.";
Case 402:
strHTTPStatusCodeDesc = "Payment Required"
Case 403:
strHTTPStatusCodeDesc = "Forbidden - The server refused to service the request.";
Case 404:
strHTTPStatusCodeDesc = "Not Found - The server cannot find the requested resource.";
Case 405:
strHTTPStatusCodeDesc = "Method Not Allowed - The method specified in the Request-Line is not permitted for the requested resource.";
Case 406:
strHTTPStatusCodeDesc = "Not Acceptable - The requested resource can only generate response entities that have unacceptable content characteristics according to the accept headers sent in the request.";
Case 407:
strHTTPStatusCodeDesc = "Proxy Authentication Required - The proxy server must authenticate the user before servicing the request.";
Case 408:
strHTTPStatusCodeDesc = "Request Timeout - The client did not make its request within the amount of time the server will wait for requests.";
Case 409:
strHTTPStatusCodeDesc = "Conflict - The server could not service the request due to a current conflict with the requested resource.";
Case 410:
strHTTPStatusCodeDesc = "Gone - The requested resource is no longer available on the server.";
Case 411:
strHTTPStatusCodeDesc = "Length Required - The server will not service the request without a Content-Length specified in the request.";
Case 412:
strHTTPStatusCodeDesc = "Precondition Failed - A precondition specified in one or more of the request-header fields failed.";
Case 413:
strHTTPStatusCodeDesc = "Request Entity Too Large - The server will not service the request because the requested resource is too large.";
Case 414:
strHTTPStatusCodeDesc = "Request-URI Too Long - The server will not service the request because the requested URL is too long.";
Case 415:
strHTTPStatusCodeDesc = "Unsupported Media Type - The server will not service the request because the format of the request is not supported by the requested resource for the requested method.";
Case 500:
strHTTPStatusCodeDesc = "Internal Server Error - The server could not service the request because of an unexpected internal error.";
Case 501:
strHTTPStatusCodeDesc = "Not Implemented - The sever cannot service the request because it does not support the request method.";
Case 502:
strHTTPStatusCodeDesc = "Bad Gateway - The proxy server received an invalid response from the content server or another proxy server in a proxy chain.";
Case 503:
strHTTPStatusCodeDesc = "Service Unavailable - The server could not service the request because it was temporarily overloaded or undergoing maintenance.";
Case 504:
strHTTPStatusCodeDesc = "Gateway Timeout - The proxy server did not receive a response from a chained proxy server or the origin content server within an acceptable amount of time.";
Case 505:
strHTTPStatusCodeDesc = "HTTP Version Not Supported - The server does not support the HTTP version specified in the request.";
Default:
strHTTPStatusCodeDesc = "Unrecognized HTTP Status Code"
}

}
else
{

switch( pintHTTPStatusCode)
{
Case 100:
strHTTPStatusCodeDesc = "Continue";
Case 101:
strHTTPStatusCodeDesc = "Switching Protocols";
Case 200:
strHTTPStatusCodeDesc = "OK";
Case 201:
strHTTPStatusCodeDesc = "Created";
Case 202:
strHTTPStatusCodeDesc = "Accepted";
Case 203:
strHTTPStatusCodeDesc = "Non-Authoritative Information";
Case 204:
strHTTPStatusCodeDesc = "No Content";
Case 205:
strHTTPStatusCodeDesc = "Reset Content";
Case 206:
strHTTPStatusCodeDesc = "Partial Content";
Case 300:
strHTTPStatusCodeDesc = "Multiple Choices";
Case 301:
strHTTPStatusCodeDesc = "Moved Permanently";
Case 302:
strHTTPStatusCodeDesc = "Moved Temporarily";
Case 303:
strHTTPStatusCodeDesc = "See Other";
Case 304:
strHTTPStatusCodeDesc = "Not Modified";
Case 305:
strHTTPStatusCodeDesc = "Use Proxy";
Case 400:
strHTTPStatusCodeDesc = "Bad Request";
Case 401:
strHTTPStatusCodeDesc = "Unauthorized";
Case 402:
strHTTPStatusCodeDesc = "Payment Required";
Case 403:
strHTTPStatusCodeDesc = "Forbidden";
Case 404:
strHTTPStatusCodeDesc = "Not Found";
Case 405:
strHTTPStatusCodeDesc = "Method Not Allowed";
Case 406:
strHTTPStatusCodeDesc = "Not Acceptable";
Case 407:
strHTTPStatusCodeDesc = "Proxy Authentication Required";
Case 408:
strHTTPStatusCodeDesc = "Request Timeout";
Case 409:
strHTTPStatusCodeDesc = "Conflict";
Case 410:
strHTTPStatusCodeDesc = "Gone";
Case 411:
strHTTPStatusCodeDesc = "Length Required";
Case 412:
strHTTPStatusCodeDesc = "Precondition Failed";
Case 413:
strHTTPStatusCodeDesc = "Request Entity Too Large";
Case 414:
strHTTPStatusCodeDesc = "Request-URI Too Long";
Case 415:
strHTTPStatusCodeDesc = "Unsupported Media Type";
Case 500:
strHTTPStatusCodeDesc = "Internal Server Error";
Case 501:
strHTTPStatusCodeDesc = "Not Implemented";
Case 502:
strHTTPStatusCodeDesc = "Bad Gateway";
Case 503:
strHTTPStatusCodeDesc = "Service Unavailable";
Case 504:
strHTTPStatusCodeDesc = "Gateway Timeout";
Case 505:
strHTTPStatusCodeDesc = "HTTP Version Not Supported";
Default
strHTTPStatusCodeDesc = "Unrecognized HTTP Status Code";
}
}

return strHTTPStatusCodeDesc;

}
}

Share |

Monday, August 27, 2007

Java "class"-ic errors



Few errors are so common (Frequently Faced Errors or FFEs?) - asked many times in forums, email lists (internal/external) - and it is frustating to face one of these "starting problems". I am talking about the classloading errors with Java. I'll try to list few common errors and most probable reasons.


  • Error occurred during initialization of VM
    java/lang/NoClassDefFoundError: java/lang/Object
    Two possible reasons:
    1. You are trying to run 64-bit JVM (SPARCv9 or AMD-64) with -d64 option. But, you did not install 32-bit JVM. Yes, to run 64-bit JVM you have to install a 32-bit JVM and then install 64-bit binary on the same directory
    2. You are playing with -Xbootclasspath option (why? - in most scenarios it is wrong!) and you didn't include rt.jar in boot class path

  • Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version number in .class file
    (or)
    Unsupported major.minor version 49.0
    Reason: You are trying to run a class compiled with newer version of JDK. For example, you have compiled the class with JDK 5.0 and try to run on JDK 1.4.2. Class file major/minor version evolves with major JDK releases. Each JDK version supports only upto a specific class file major.minor version.
    1. Java SE 6 class file version 50.0 or below
    2. JDK 5.0 class file version 49.0 or below
    3. JDK 1.4.2 class file version 48.0 or below
    While you can run a .class file compiled with past release of JDK, the other way around does not work. You can use javac's -target option. Also, always check version of "java" used to run your program by using -version option.

  • Exception in thread "main" java.lang.NoClassDefFoundError: tmp/t (wrong name: t)
    Reason: The class name as in .class file is different from what is specified in command line. In the above error scenario, the .class file has the name "t" (in unnamed global package) but user attempted to load it as "tmp.t" (t in tmp package). You have to check package declaration in your source files and confirm that the files are under proper subdirectories (use javac's -d option to put .class files under correct subdirectories). Also you may want to check directory, file name case -- remember to do this on case insensitive file systems as well. It is better to use jar files whenever possible (rather than directories)

  • Exception in thread "main" java.lang.NoClassDefFoundError: x
    This is good (bad?) old error! It is better to check
    1. the value of CLASSPATH environment variable
    2. the value passed as -classpath or -cp option
    3. check that proper path separator (; on Windows, : on Unix - it is easy to miss or use wrong separator for your OS) is used in your classpath specification
    4. Remember that "." (the current directory) is included by default in classpath - but if you have specified CLASSPATH, then that overrides. i.e., With CLASSPATH specified, "." is not automatically added to classpath.
    5. With Mustang (Java SE 6) you can put all your all jar files in one or fewer directories and use classpath wildcards to avoid typos in jar file names.
    6. When in doubt, check again before posting to forums :-) Yes, typos are quite common :-)
When you are out of starting troubles, you may still face other classloading issues. "unexpected" class is loaded etc. How can you debug that?
  • Turn on -verbose:class. This prints debug info on each class load. The source of the class file loaded (rt.jar , foo.jar) is also printed along that.
  • Check your configuration. There are many ways to pass arguments to running JVM. (command line argument, environment variables, config files of app/web servers). You may want to check what actual arguments were "seen" by the JVM. You can check these using
    1. jps
    2. jinfo
    The value of the System property java.class.path is the application classpath used. Similarly, the value of the System property sun.boot.class.path is value of bootstrap class path (Sun specific).

Share |

Topics

 
Embed Wave to Blogger