1

HTTP パラメータを使用してクエリをサーバーに送信し、結果を取得しています。Jsoup を使用し、URL でパラメーターを送信します。

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

ただし、結果が必要ない場合もあります。パラメータをサーバーに送信し、応答を無視したいだけです。

Jsoupが応答をダウンロードするのを防ぐ方法はありますか? 応答をダウンロードせずに HTTP URL パラメータをサーバーに送信する他の方法はありますか?

4

1 に答える 1

1

Jsoup は下で HttpURLConnection を使用します。応答を省略することはオプションではありませんし、そうすべきでもありません。これは解析フレームワークであるためです。そうしないと意味がありません。あなたができることは、リクエストを送信するために HttpURLConnection を直接使用することです。その後、レスポンスを読み取らないだけです。ここから取った POST と GET の例は次のとおりです。

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());*/

    }

}
于 2014-07-23T04:50:14.067 に答える