带有效负载的Jsoup HTTP POST [英] Jsoup HTTP POST with payload

查看:164
本文介绍了带有效负载的Jsoup HTTP POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to make this http request via Jsoup: http://api.decarta.com/v1/[KEY]/batch?requestType=geocode as given here. And here is my code for the same:

                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()给出:

The jsonPayload.toString() gives this:

{"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。

Which is a perfectly valid json.

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

However Jsoup each time returns HTTP status 400(malformed). So how do I send proper http POST with JSON payload using Jsoup if at all this is possible.(Pls note that its payload and not an ordinary key-val pair in url).

推荐答案

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

What you need is to post raw data. That functionality has been implemented but it hasn't been added yet. Check this pull request https://github.com/jhy/jsoup/pull/318 . Do you really need to use jsoup for this? I mean you could use HttpURLConnection (this is what jsoup uses underneath) to make the request and then pass the response to jsoup as a string.

以下是来自 HttpURLConnection 的一个例子(但是简化并添加了json / raw数据) http://www.mkyong.com/java/java-httpurlconnection-follow-redirect-example/\"rel =nofollow> www.mkyong.com

Here is an example of HttpURLConnection taken (but simplified and added json/raw data) from www.mkyong.com

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

这篇关于带有效负载的Jsoup HTTP POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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