Sending a HTTP request from Java code
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
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—–“);
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();
}
}
}
e.printStackTrace();
}
}
}
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