How to send data in jsoup connection?

拈花ヽ惹草 提交于 2019-12-04 19:14:24

This command

curl coordiutil.com/rpc/server.php -v -H "Accept:application/json" -H "Accept-Encoding:gzip,deflate,sdch" -H "Accept-Language:es-ES,es;q=0.8" -H "Connection:keep-alive" -H "X-Requested-With:XMLHttpRequest" -d '{"method":"Catalog.search","params":{"pag":1,"business_url":"tecnologia","categ‌​ory_url":"conectividad","subcategory_url":"","valmin":-1,"valmax":-1}}:' -H "Content-Length:149"

could be something like this, but jsoup will use URLEncoder.encode() on the data.

    public static void main(String[] args) {
        try {
            Document doc = Jsoup.connect("http://www.coordiutil.com/rpc/server.php")
                            //.userAgent("curl/7.34.0")
                            .header("Accept", "application/json")
                            .header("Accept-Encoding", "gzip,deflate,sdch")
                            .header("Accept-Language", "es-ES,es;q=0.8")
                            .header("Connection", "keep-alive")
                            .header("X-Requested-With", "XMLHttpRequest")
                            //.header("Content-Type", "application/x-www-form-urlencoded")
                            .data("{\"method\":\"Catalog.search\",\"params\":{\"pag\":1,\"business_url\":\"tecnologia\",\"categ‌​ory_url\":\"conectividad\",\"subcategory_url\":\"\",\"valmin\":-1,\"valmax\":-1}}", "")
                            .post(); 

            System.out.println(doc.toString());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Since you are sending a rather odd POST request, just use HttpURLConnection, without Jsoup on top of it.

        String rawData = "{\"method\":\"Catalog.search\",\"params\":{\"pag\":\"1\",\"business_url\":\"tecnologia\",\"categ‌​ory_url\":\"conectividad\",\"subcategory_url\":\"fkdj\",\"valmin\":\"-1\",\"valmax\":\"-1\"}}";
        String url = "http://www.coordiutil.com/rpc/server.php";
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();

        //add reuqest header
        con.setRequestMethod("POST");
        con.setRequestProperty("Accept", "application/json");
        con.setRequestProperty("Accept-Encoding", "gzip,deflate,sdch");
        con.setRequestProperty("Accept-Language", "es-ES,es;q=0.8");
        con.setRequestProperty("Connection", "keep-alive");
        con.setRequestProperty("X-Requested-With", "XMLHttpRequest");

        // Send post request
        con.setDoOutput(true);

        OutputStreamWriter w = new OutputStreamWriter(con.getOutputStream(), "UTF-8");

        w.write(rawData);
        w.close();

        int responseCode = con.getResponseCode();

        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 code : " + responseCode);
        System.out.println(response.toString());

The latest jsoup's release (1.10.2) has the Connection#requestBody method, that allow the POST's execution using a JSON as body. The code should look something like that:

  Connection connection;
  Response response;
  String url = "url of .php";
  String body = "{\n"
          + "   \"method\": \"Catalog.search\",\n"
          + "   \"params\": {\n"
          + "       \"pag\": 1,\n"
          + "       \"business_url\": \"electrodomesticos\",\n"
          + "       \"category_url\": \"climatizacion\",\n"
          + "       \"subcategory_url\": \"\",\n"
          + "       \"valmin\": -1,\n"
          + "       \"valmax\": -1\n"
          + "   }\n"
          + "}";

  connection = Jsoup.connect(url)
          .userAgent("Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36") // User-Agent of Chrome 55
          .referrer("my referer") // Custom Referer
          .header("Accept", "application/json")
          .header("Accept-Encoding", "gzip,deflate,sdch")
          .header("Accept-Language", "es-ES,es;q=0.8")
          .header("Connection", "keep-alive")
          .header("X-Requested-With", "XMLHttpRequest")
          .requestBody(body)
          .maxBodySize(0)
          .timeout(0)
          .method(Method.POST); // Infinite / Milis time

  response = connection.execute();

try this:

 String[] POST_DATA = new String[]{KEY, VALUE, KEY2, VALUE2}; 
    Response response = Jsoup.connect(URL)
    .data(POST_DATA )
    .method(Connection.Method.POST)
    .execute;

reference: https://jsoup.org/apidocs/org/jsoup/Connection.html#requestBody-java.lang.String-

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!