带有效负载的Jsoup HTTP POST

时间:2014-12-13 20:00:16

标签: java json http-post jsoup

我正在尝试通过Jsoup发出此http请求:http://api.decarta.com/v1/[KEY]/batch?requestType=geocode给定here。 这是我的相同代码:

                String postUrl=postURLPrefix+apiKey+"/batch?requestType=geocode";
                System.out.println(postUrl);
                String response= Jsoup.connect(postUrl).timeout(60000).ignoreContentType(true)
                        .header("Content-Type", "application/json;charset=UTF-8")
                        .method(Connection.Method.POST)
                        .data("payload",jsonPayload.toString())
                        .execute()
                        .body();

jsonPayload.toString()给出了这个:

{"payload":["146 Adkins Street,Pretoria,Pretoria,Gauteng","484 Hilda Street,Pretoria,Pretoria,Gauteng","268 Von Willich Street,Centurion,Centurion,Gauteng","100 Lion Road,Pretoria,Pretoria,Gauteng","Poligoon Street,Pretoria,Pretoria,Gauteng","91 Hornbill Street,Pretoria,Pretoria,Gauteng","55 Eland Street,Pretoria,Pretoria,Gauteng","31 Pelican Avenue,Centurion,Centurion,Gauteng","308 The Hillside Street,Pretoria,Pretoria,Gauteng","8 Spekhout Avenue,Centurion,Centurion,Gauteng","108 Apiesdoring Street,Pretoria,Pretoria,Gauteng","521 Louis Trichardt Street,Pretoria,Pretoria,Gauteng","31 Leopard Road,Pretoria,Pretoria,Gauteng","648 Klippan Street,Pretoria,Pretoria,Gauteng","13 Sweetpea Avenue,Pretoria,Pretoria,Gauteng","232 Kemphaan Street,Centurion,Centurion,Gauteng","32 Cantonments Road,Centurion,Centurion,Gauteng","882 Burlington St,Roseville,Gauteng","15 Brits Street,Olympus Ridge Complex,Centurion,Gauteng","15 Brits Street,Monument Park,Centurion,Gauteng","35 De La Rey Road,Monument Park,Centurion,Gauteng","112 Diamond St,Monument Park,Klerksoord,Gauteng","Hendrik Verwoerd Drive,Lyttelton,Centurion,Gauteng","777 Gambry Avenue,Garsfontein,Pretoria,Gauteng","57 Pheasant Avenue,Waterkloof Rand Corporatepark,Akasia,Gauteng","18 Huilboom Street,Manitoba Mews,Pretoria,Gauteng","75 Gousblom Avenue,Euro Stadt,Pretoria,Gauteng","88 Cambridge Avenue,Garsfontein,Centurion,Gauteng","662 Pual Kruger Street,Olympus Ridge Complex,Pretoria,Gauteng","231 Charles Street,Hatfield,Pretoria,Gauteng","9 Kobus Street,Pretoria West,Centurion,Gauteng","96 Siersteen Road,Byron Place,Pretoria,Gauteng","262 Molopo Avenue,Montana Crossing,Pretoria,Gauteng","171 Sonja Street,Moreleta Park,Centurion,Gauteng","751 Lucas Meyer Street,Moreleta Park,Pretoria,Gauteng","499 Moot Street,Centurion Lifestyle Centre,Pretoria,Gauteng","4 Hofsanger Road,Villa Lanei,Centurion,Gauteng","51 Newark Street,Centurion,Gauteng","25 Anton Street,Lyttelton,Centurion,Gauteng","15 Brits Street,Garsfontein Ext 10,Centurion,Gauteng","172 Wildeamandel Street,La Motagne,Pretoria,Gauteng","15 Fillicia Street,Waterkloof,Pretoria,Gauteng","20 Slagveld Street,Centurion,Gauteng","678 Rankdoring Street,Waterkloof Glen,Pretoria,Gauteng","7 Hillips Street,Faerie Glen X 34,Pretoria,Gauteng","59 Malherbe Street,Willows,Pretoria,Gauteng","204 Festival Street, Unit 1\",Willows,Pretoria","310 Cliff Avenue,Manitoba Mews,Pretoria,Gauteng","294 Panorama Road,Hatfield,Centurion,Gauteng","79 Buitenkant Street,Opera Plaza,Pretoria,Gauteng"]}

这是一个完全有效的json。

然而,Jsoup每次返回HTTP状态400(格式错误)。因此,如果可能的话,如何使用Jsoup发送具有JSON有效负载的正确http POST。(请注意其有效负载而不是url中的普通key-val对。)

2 个答案:

答案 0 :(得分:5)

使用最新的JSOUP库。

如果您使用的是maven,请将以下条目添加到pom.xml

String postUrl=postURLPrefix+apiKey+"/batch?requestType=geocode";
            System.out.println(postUrl);
            String response= Jsoup.connect(postUrl).timeout(60000).ignoreContentType(true)
                    .method(Connection.Method.POST)
                    .requestBody("payload",jsonPayload.toString())
                    .execute()
                    .body();

以下代码可以解决您的问题。

{{1}}

答案 1 :(得分:4)

您需要发布raw data。该功能已经实施,但尚未添加。检查此提款请求https://github.com/jhy/jsoup/pull/318。你真的需要使用jsoup吗?我的意思是你可以使用HttpURLConnection(这是jsoup在下面使用的)来发出请求,然后将响应作为字符串传递给jsoup。

以下是HttpURLConnection

import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; public class Main { public static void main(String[] args) { try { String url = "http://www.google.com"; URL obj = new URL(url); HttpURLConnection conn = (HttpURLConnection) obj.openConnection(); conn.setReadTimeout(5000); conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8"); conn.addRequestProperty("User-Agent", "Mozilla"); conn.addRequestProperty("Referer", "google.com"); conn.setDoOutput(true); OutputStreamWriter w = new OutputStreamWriter(conn.getOutputStream(), "UTF-8"); w.write("SOME_JSON_STRING_HERE"); w.close(); System.out.println("Request URL ... " + url); int status = conn.getResponseCode(); System.out.println("Response Code ... " + status); BufferedReader in = new BufferedReader(new InputStreamReader( conn.getInputStream())); String inputLine; StringBuffer html = new StringBuffer(); while ((inputLine = in.readLine()) != null) { html.append(inputLine); } in.close(); conn.disconnect(); System.out.println("URL Content... \n" + html.toString()); System.out.println("Done"); } catch (Exception e) { e.printStackTrace(); } } } 采取(但简化并添加了json / raw数据)的示例
{{1}}