How to send data in jsoup connection?

无人久伴 提交于 2019-12-06 13:34:00

问题


I need to send data in jsoup connection request. This is the Form Data that I can see in chrome developer console.

{"method":"Catalog.search","params":{"pag":1,"business_url":"electrodomesticos","category_url":"climatizacion","subcategory_url":"","valmin":-1,"valmax":-1}}

This is my code for doing this

    String phpUrl = "url of .php";
    Connection conn = Jsoup.connect(phpUrl).userAgent("Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36").referrer(referer).maxBodySize(0).timeout(Main.TIMEOUT);

    Map  <String,String>  myMap= new HashMap <String, String>();
    myMap.put("method", "Catalog.search");
    //myMap.put("params", "{}");
    myMap.put("pag", "1");
    myMap.put("business_url", "electrodomesticos");
    myMap.put("category_url", "climatizacion" );
    myMap.put("subcategory_url", "" );
    myMap.put("valmin", "-1" );
    myMap.put("valmax", "-1");
    conn.data(myMap);
    conn.post();
    Connection.Response respon = conn.execute();

I tried few more combinations but I aways get http 500 error. I know that my syntax is wrong. So please can somebody tell me the right syntax to send that data.


回答1:


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



回答2:


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



回答3:


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-



来源:https://stackoverflow.com/questions/24871625/how-to-send-data-in-jsoup-connection

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