Powered by Blogger.

Wednesday 11 February 2015

HTTP URL

No comments :
HttpURLConnection :
Java provides a subclass of URLConnection that provides support for HTTP connections. This class is called HttpURLConnection. You obtain an HttpURLConnection in the same way just shown, by calling openConnection() on a URL object, but you must cast the result to HttpURLConnection. you can also use any of the several Methods defined by HttpURLConnection.

static boolean getFollowRedirects()Returns true if redirects are automatically followed and false otherwise. This feature is on by default.
String getRequestMethod()Returns a string representing how URL requests are made. The default is GET. Other options, such as Post, are available.
int getResponseCode() throws IOExceptionReturns the HTTP response code -1 is returned if no response code can be obtained. As IOException is thrown if the connection fails.
String getResponseMessage() throws IOException Returns the response message associated with the response code. Returns NULL if no message is available. An IOException is throws if the connection fails.
static void setFollowRedirects(boolean how)if how is true, then redirects are automatically followed. if how is false, redirects are not automatically followed. By default, redirects are automatically followed.
void setRequestMethod(String how) throws ProtocolExceptionSet the method by which HTTP requests are made to that specified by how. the default method is GET, but other options, such as POST, are available. if how is invalid, a ProtocolException is thrown.

The Following program demonstrates HttpURLConnection. It first establishes a connection to "www.a143mkblogspot.in". Then it displays the request method, the response code and The Response Message.



Out Put:
Request Method is :GET 
Response Code is : 200 
Response Message is : OK 
Here is the Header : Key : 
Transfer-EncodingValues[chunked] 
Key : nullValues[HTTP/1.1 200 OK] 
Key : ServerValues[GSE] 
Key : X-Content-Type-OptionsValues[nosniff] 
Key : Last-ModifiedValues[Wed, 11 Feb 2015 10:56:58 GMT] 
Key : DateValues[Wed, 11 Feb 2015 10:57:00 GMT] 
Key : Alternate-ProtocolValues[80:quic,p=0.02] 
Key : Accept-RangesValues[none] 
Key : Cache-ControlValues[private, max-age=0] 
Key : VaryValues[Accept-Encoding] 
Key : ExpiresValues[Wed, 11 Feb 2015 10:57:00 GMT] 
Key : X-XSS-ProtectionValues[1; mode=block] 
Key : Content-TypeValues[text/html; charset=UTF-8] 

No comments :

Post a Comment

Please Write a Message for Programming or something Related issues.