Docsity

URL Programming: Fetching and Submitting HTTP Requests in Java - Prof. Ala I. Al-Fuqaha, Study notes of Computer Science

Partial preview of the text

Download URL Programming: Fetching and Submitting HTTP Requests in Java - Prof. Ala I. Al-Fuqaha and more Study notes Computer Science in PDF only on Docsity!

URL Programming

Introduction

„ How to write a program that fetches

URL content

„ How to write a program that submits

GET or POST requests to a web server

or a servlet

Start simply

„ No programming involved!

Connect to server (telnet)

Send request (type in an http request)

Get input

„ Using TCP Sockets

Connect to port 80

Send HTTP request (GET, POST, HEAD)

Read HTTP response

URL Programming In Java

„ A URL is a name for a web resource (page, applet)

http://www.cs.wmich.edu/index.html

„ Java URL programming allows Java programs to accessweb resources by sending protocol requests and

receiving protocol responses.

„ Related Classes:

URL classURLConnection class

HttpURLConnection class

URLEncoder class

Retrieving URL contents

„ There are a number of ways to do this:

Object getContent();

InputStream openStream();

URLConnection openConnection();

Getting Header Information

„ There are methods that return information extracted from response headers:

String getContentType();

String getContentLength();

long getLastModified();

URLConnection Class

„ 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);

Faking GET Form Submission

„ 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

Faking POST Form Submission

„ 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

URLEncoder Class

„ 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