Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.URL;
- public class HttpURLConnectionExample {
- private final String USER_AGENT = "Mozilla/5.0";
- public static void main(String[] args) throws Exception {
- HttpURLConnectionExample http = new HttpURLConnectionExample();
- System.out.println("Testing 1 - Send Http GET request");
- System.out.println(http.sendGet("http://m.mytv.vn/"));
- }
- // HTTP GET request
- private String sendGet(String url) throws Exception {
- URL obj = new URL(url);
- HttpURLConnection con = (HttpURLConnection) obj.openConnection();
- // optional default is GET
- con.setRequestMethod("GET");
- // add request header
- con.setRequestProperty("User-Agent", USER_AGENT);
- int responseCode = con.getResponseCode();
- System.out.println("\nSending 'GET' request to URL : " + url);
- System.out.println("Response Code : " + responseCode);
- BufferedReader in = new BufferedReader(new InputStreamReader(
- con.getInputStream()));
- String inputLine;
- StringBuffer response = new StringBuffer();
- while ((inputLine = in.readLine()) != null) {
- response.append(inputLine);
- }
- in.close();
- // print result
- return response.toString();
- // System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment