HttpURLConnectionDemo.javaJava documentation HttpURLConnection class
// Demonstrate HttpURLConnection.
import java.net.*;
import java.io.*;
import java.util.*;
class HttpURLConnectionDemo
{
public static void main(String[] args) {
try {
URL url = new URL("http://www.udayton.edu");
HttpURLConnection connection =
(HttpURLConnection) url.openConnection();
// Display request method.
System.out.println("Request method is " +
connection.getRequestMethod());
// Display response code.
System.out.println("Response code is " +
connection.getResponseCode());
// Display response message.
System.out.println("Response Message is " +
connection.getResponseMessage());
// Get a list of the header fields and a set
// of the header keys.
Map<String, List<String>> hdrMap = connection.getHeaderFields();
Set<String> hdrKeys = hdrMap.keySet();
System.out.println("\nHere is the header:");
// Display all header keys and values.
for(String k : hdrKeys) {
System.out.println("Key: " + k +
" Value: " + hdrMap.get(k));
}
} catch(IOException exc) {
System.out.println(exc);
}
}
}
Request method is GET Response code is 200 Response Message is OK Here is the header: Key: null Value: [HTTP/1.1 200 OK] Key: Transfer-Encoding Value: [chunked] Key: Date Value: [Thu, 15 Nov 2012 01:56:03 GMT] Key: Expires Value: [Thu, 19 Nov 1981 08:52:00 GMT] Key: Via Value: [1.1 www.udayton.edu (Access Gateway 3.1.4-27-CEFBE9766872A0DB-29028)] Key: Set-Cookie Value: [ZNPCQ003-32393400=c704e451; path=/; domain=.udayton.edu, PHPSESSID=a6265coqtimm029vu02e93stqeor5aq2; path=/; HttpOnly] Key: Content-Type Value: [text/html] Key: X-Powered-By Value: [PHP/5.2.14] Key: Server Value: [Apache/2.2.12 (Linux/SUSE)] Key: Cache-Control Value: [no-cache, no-store, no-store, no-cache, must-revalidate, post-check=0, pre-check=0] Key: Pragma Value: [no-cache]
Maintained by John Loomis, updated Wed Nov 14 20:55:46 2012