вторник, 30 ноября 2010 г.

Как отправить GET-запрос в Java?



public String sendGetRequest(String url, String requestParams)
{
    String result = null;
    if (endpoint.startsWith("http://")) {
        // Send a GET request to the servlet
        try {
            // Construct data
            StringBuffer data = new StringBuffer();
            // Send data
            String urlStr = endpoint;
            if (requestParameters != null && requestParameters.length () > 0) {
                urlStr += "?" + requestParameters;
            }
            URL url = new URL(urlStr);
            URLConnection conn = url.openConnection ();
            // Get the response
            BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            StringBuffer sb = new StringBuffer();
            String line;
            while ((line = rd.readLine()) != null) {
                sb.append(line);
            }
            rd.close();
            result = sb.toString(); 
    } 
    catch (Exception e) {
        e.printStackTrace();
    }
}
return result;
}

Комментариев нет:

Отправить комментарий