Sending a HTTP request from Java code

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
public class HTTPRequest {
public static void main(String[] args) {
String requestUrl = http://www.google.com.hk;
try {
URL url = new URL(requestUrl.toString());
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String inputLine;
System.out.println(—–RESPONSE START—–);
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
in.close();
System.out.println(—–RESPONSE END—–);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Bookmark and Share
Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks

Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.

Comments

No comments yet.

Leave a comment

(required)

(required)


Anti-Spam Protection by WP-SpamFree