我需要在jsoup连接请求中发送数据。这是我在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错误。我知道我的语法错了。所以请有人告诉我发送该数据的正确语法。
答案 0 :(得分:0)
此命令
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","category_url":"conectividad","subcategory_url":"","valmin":-1,"valmax":-1}}:' -H "Content-Length:149"
可能是这样的,但jsoup将对数据使用URLEncoder.encode()。
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\",\"category_url\":\"conectividad\",\"subcategory_url\":\"\",\"valmin\":-1,\"valmax\":-1}}", "")
.post();
System.out.println(doc.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
由于您发送了一个相当奇怪的POST请求,只需使用HttpURLConnection,而不使用Jsoup。
String rawData = "{\"method\":\"Catalog.search\",\"params\":{\"pag\":\"1\",\"business_url\":\"tecnologia\",\"category_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());
答案 1 :(得分:0)
latest jsoup's release (1.10.2)具有Connection#requestBody
方法,允许使用JSON作为正文执行POST。代码看起来应该是这样的:
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();
答案 2 :(得分:0)
试试这个:
String[] POST_DATA = new String[]{KEY, VALUE, KEY2, VALUE2};
Response response = Jsoup.connect(URL)
.data(POST_DATA )
.method(Connection.Method.POST)
.execute;
参考: https://jsoup.org/apidocs/org/jsoup/Connection.html#requestBody-java.lang.String-