我正在尝试通过给定的 jsoup 发出此 HTTP 请求 here :
http://api.decarta.com/v1/[KEY]/batch?requestType=geocode
这是我的代码:
String postUrl = postURLPrefix + apiKey + "/batch?requestType=geocode";
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",
...
]
}
这是一个完全有效的 JSON。
但是,jsoup 每次都返回 HTTP 状态代码 400(格式错误)。
那么,如果可能的话,我该如何使用 jsoup 发送带有 JSON 负载的正确 HTTP POST? (请注意,它是有效负载,而不是 URL 中的普通键值对)
最佳答案
使用最新的 JSOUP 库。
如果您使用的是 maven,则将以下条目添加到 pom.xml
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.10.2</version>
</dependency>
下面的代码将解决您的问题。
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();
关于java - 带有负载的 Jsoup HTTP POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27463036/