





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






Connect to server (telnet)
Send request (type in an http request)
Get input
Connect to port 80
Send HTTP request (GET, POST, HEAD)
Read HTTP response
URL Programming In Java
http://www.cs.wmich.edu/index.html
URL classURLConnection class
HttpURLConnection class
URLEncoder class
Retrieving URL contents
There are a number of ways to do this:
There are methods that return information extracted from response headers:
Represents the connection (not the URL itself). More control than URL
can write to the connection (send POST data).
can set request headers.
HttpURLConnection is a subclass of URLConnection that is specific to HTTP
Example: Reading from a URL //URL -- url open = newconnection URL(“http://www.google.com/index.html”);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.connect(); //BufferedReader -- read inin = htmlnew BufferedReader(new ---------------- InputStreamReader(conn.getInputStream()));
StringString line;page = new String(""); while (null != ((line =page += line; in.readLine()))) { } in.close();System.out.println(page);
Not all webpages are produced in response to a simple GET request Some are the output of a program
which may require parameters
passed using the GET method (via the URL), e.g. http://www.cs.wmich.edu/servlet/convert?id=57&amount=10&units=pint
The POST request sends parameters (data) in the HTTP request body Any data to be sent must be encoded as a string of key-value pairs: id=57&amount=10&units=pint
Used to encode GET and POST parameters. Example: String content = "action=“ + URLEncoder.encode("100”); content += "&zipcode=“ + URLEncoder.encode(“49008"); content += "&name=“ + URLEncoder.encode(“Bill Somebody"); content += "&passwd=“ + URLEncoder.encode(“cisco");
Step 1: Encode GET parameters String content = "action=“ + URLEncoder.encode("100”); content += "&zipcode=“ + URLEncoder.encode(“49008"); content += "&name=“ + URLEncoder.encode(“Bill Somebody"); content += "&passwd=“ + URLEncoder.encode(“cisco"); Step 2: Make an HttpURLConnection object in the usual way: URL url = new URL("http://www.cs.wmich.edu/servlet/converter“ + “?” + contents); Step 3: HttpURLConnection conn = (HttpURLConnection)url.openConnection(); Step 4: conn.connect(); Step 5: You can read the reply as explained before.
Making GET Form Submissions