【问题标题】:HttpPost request does not send correct JSON dataHttpPost 请求未发送正确的 JSON 数据
【发布时间】:2013-08-06 13:30:08
【问题描述】:

我目前正在尝试按照fliptop 的说明使用 API。 api的描述指定它必须使用json输入,因此,我自己制作了json对象并将其转换为字符串。但是,当我尝试使用 find_contact api 时,http 响应会得到 ​​p>

{"param":500}

而不是像

这样的正确数据

{ “帐户”: { "site_key": "...", "seat_id": "...", “org_id”:“”, “执照”: { ... }。

我尝试通过发送带有错误输入的请求在文档网站上进行一些测试(单击任何高级api,你会看到它),它给了我相同的响应正文{ “参数”:500}。因此,我认为我的请求实体可能有问题。

这是我的代码。谁能告诉我这是怎么回事?

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

public class Test {

    // Generate json string
    // {"contacts": [ { "email": "doug@fliptop.com", "title":"CEO", "company":"Fliptop", "address":"San Francisco"} ] }
    public static String jsonString() {
        JSONObject obj1 = new JSONObject();
        obj1.put("email", "doug@fliptop.com");
        obj1.put("title", "CEO");
        obj1.put("company", "Fliptop");
        obj1.put("address", "San Francisco");
        JSONArray list1 = new JSONArray();
        list1.add(obj1);
        JSONObject obj2 = new JSONObject();
        obj2.put("contacts", list1);
        System.out.println(obj2.toJSONString());
        return obj2.toJSONString();
    }

    public static void find_contact() {
        HttpClient httpClient = new DefaultHttpClient();
        try {
                    //you will need api key here!!
            HttpPost request = new HttpPost("http://api.fliptop.com/s2/v1/find_contact?api_key=xxxxxxxxxxxxxxxxxxxxxx");
            StringEntity params = new StringEntity(jsonString(),"UTF-8");
            params.setContentType("application/json; charset=UTF-8");
            request.setEntity(params);
            HttpResponse response = httpClient.execute(request);
            BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            String result = "";
            String line;
            while ((line = rd.readLine()) != null) {
                result += line;
            }
            System.out.println(result);
            rd.close();
            System.out.println("status:" + response.getStatusLine());
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String args[]) {
        find_contact();
    }
}

更新:

我按照 Dave 的建议检查了 WireShark 的请求。请求显示为POST请求,数据为text/plain,内容如下

json={"contacts": [ { "email":"doug@fliptop.com", "title":"CEO", "company":"Fliptop", "address":"旧金山"} ] } .

我不是网络专家,但这个请求对我来说似乎很好。尽管如此,我还是对发生了什么感到困惑......

【问题讨论】:

  • 您是否检查了线路以查看您实际发送的内容?
  • 我真的不明白你想说什么...我确实尝试通过将实体放回 InputStreamReader 然后 BufferedReader 来读取实体。实体字符串本身是正确的。
  • 我的意思是“通过检查发出的请求来检查您对实际发送到服务器的内容的假设”。
  • 我确实在我的代码中使用了正确的 api 密钥。如果这是您的建议,我不会将我的 api 密钥放在此演示代码中。否则,我仍然不知道你想告诉我什么。
  • 我试图告诉您通过检查网络上实际发送的内容来检查实际发出的请求;调试 101。

标签: java json web-services post


【解决方案1】:

又挣扎了几个小时后,我通过检查来自 chrome 的 post 请求的详细信息发现了问题的原因。您可以看到 Content-Type 实际上是 "application/x-www-form-urlencoded",这就是我一直失败的原因。通过更改我的 Content-Type,一切正常。

这是我的代码,你可以使用 URL 或 appache httpclient,它们都可以正常工作。

    URL url = new URL("http://api.fliptop.com/s2/v1/find_contact?api_key=xxxxxxxxxxxxxxxxxxxxxx");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();

    connection.setRequestMethod("POST");
    connection.setDoOutput(true);
    connection.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
    writer.write(data);  // data = "json={\"contact\": { \"email\": \"robbie@fliptop.com\"}}"
    writer.close();

    if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {        
        BufferedReader reader =
            new BufferedReader(new InputStreamReader(connection.getInputStream(), UTF_8));

        String line;
        while ((line = reader.readLine()) != null) {
            result += line;
        }       
    }

【讨论】:

    猜你喜欢
    • 2018-05-14
    • 2015-09-23
    • 2019-12-21
    • 2012-07-28
    • 1970-01-01
    • 2020-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多