Java HTTP Request sendGet(String url, String param)

Here you can find the source of sendGet(String url, String param)

Description

send Get

License

Apache License

Declaration

public static String sendGet(String url, String param) throws Exception 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    public static String sendGet(String url, String param) throws Exception {
        String result = "";
        BufferedReader in = null;
        HttpURLConnection conn = null;
        try {//from ww  w  .j  a  va2s . c o  m
            String urlNameString = url + "?" + param;
            URL realUrl = new URL(urlNameString);

            conn = (HttpURLConnection) realUrl.openConnection();
            conn.setConnectTimeout(10000);
            conn.setReadTimeout(5000);

            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

            conn.connect();

            in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null)
                result = result + line;
        } catch (Exception e) {
            throw e;
        } finally {
            try {
                if (in != null)
                    in.close();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
            if (conn != null) {
                conn.disconnect();
            }
        }
        return result;
    }
}

Related

  1. request(String url, Map cookies, Map parameters)
  2. requestData(String url)
  3. requestDataFromUrl(URL url, byte[] tosend, String userAgent)
  4. saveHttpImage(String requestUrl, String requestMethod, String outputStr, File target)
  5. sendGet(String url)
  6. sendGetRequest(String requestURL)
  7. sendGetRequest(String url, String cookies)