如何在jsoup连接中发送数据? [英] How to send data in jsoup connection?

查看:152
本文介绍了如何在jsoup连接中发送数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在jsoup连接请求中发送数据。这是我可以在chrome开发者控制台中看到的表单数据。

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}}

这是我的代码。

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

我尝试了更多的组合,但我得到http 500错误。我知道我的语法错了。

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.

推荐答案

此命令

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"

可能是这样,但jsoup会对数据使用URLEncoder.encode()。

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

由于您发送了一个相当奇怪的POST请求,请使用HttpURLConnection,而不要使用Jsoup。

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

这篇关于如何在jsoup连接中发送数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆