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

                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 对)。

最佳答案

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

这是从 www.mkyong.com 获取的 HttpURLConnection 示例(但简化并添加了 json/raw 数据)

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

关于java - 带有负载的 Jsoup HTTP POST,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27463036/

10-13 08:46