1

I'm using HTTP parameters to send a query to a server and fetch the results. I use Jsoup and send my parameters in the URL:

String doc = Jsoup.connect("http://server.com/query?a=x").execute().body();

However, in some cases, I don't want the results. I just want to send the parameter to the server and ignore the response.

Is there anyway to prevent Jsoup to download the response? Is there any other way to send HTTP URL parameters to the server without downloading the response?

Saba Jamalian
  • 750
  • 2
  • 10
  • 24
  • In what cases don't you need the response? For your question, the is answer is no. There is no way without changing the source code of jsoup. – Alkis Kalogeris Jul 22 '14 at 21:18
  • @alkis Just want to trigger a procedure in the server. How about any other library/method to use instead of Jsoup? – Saba Jamalian Jul 22 '14 at 21:45

1 Answers1

1

Jsoup uses HttpURLConnection underneath. Omitting the response is not an option and nor should be, since it is a parsing framework. It wouldn't make any sense otherwise. What you can do is use HttpURLConnection directly for sending the request and then you just don't read the response. An example for POST and GET taken from here follows

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");
        http.sendGet();

        System.out.println("\nTesting 2 - Send Http POST request");
        http.sendPost();

    }

    // HTTP GET request
    private void sendGet() throws Exception {

        String url = "http://www.google.com/search?q=mkyong";

        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);

        /*This part should be removed*/
        /*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
        System.out.println(response.toString()); */

    }

    // HTTP POST request
    private void sendPost() throws Exception {

        String url = "https://selfsolve.apple.com/wcResults.do";
        URL obj = new URL(url);
        HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

        //add reuqest header
        con.setRequestMethod("POST");
        con.setRequestProperty("User-Agent", USER_AGENT);
        con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

        String urlParameters = "sn=C02G8416DRJM&cn=&locale=&caller=&num=12345";

        // Send post request
        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.writeBytes(urlParameters);
        wr.flush();
        wr.close();

        int responseCode = con.getResponseCode();
        System.out.println("\nSending 'POST' request to URL : " + url);
        System.out.println("Post parameters : " + urlParameters);
        System.out.println("Response Code : " + responseCode);

        /*This part should be removed*/
        /*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
        System.out.println(response.toString());*/

    }

}
Alkis Kalogeris
  • 17,044
  • 15
  • 59
  • 113