【问题标题】:Apache HttpClient JSON PostApache HttpClient JSON 帖子
【发布时间】:2015-03-14 02:26:08
【问题描述】:

我尝试在应用程序 (SWT/JFace) 中使用 HttpClient 4.4 直接发送 JSON 字符串:

    public String postJSON(String urlToRead,Object o) throws ClientProtocolException, IOException {
    String result="";
    CloseableHttpClient httpClient = HttpClients.createDefault();
    try {
        HttpPost postRequest = new HttpPost(urlToRead);
        postRequest.setHeader("content-type", "application/x-www-form-urlencoded");
        //postRequest.setHeader("Content-type", "application/json");

        //{"mail":"admin@localhost", "password":"xyz"}
        String jsonString=gson.toJson(o);
        StringEntity params =new StringEntity(jsonString);
        params.setContentType("application/json");
        params.setContentEncoding("UTF-8");
        postRequest.setEntity(params);
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        result = httpClient.execute(postRequest, responseHandler);
    }finally {
        httpClient.close();;
    }
    return result;
}

我尝试使用 $POST 从服务器 (Apache/PHP) 获取响应

$POST 的正确内容应该是:

array("mail"=>"admin@localhost","password"=>"xyz")

当我使用content-type : application/x-www-form-urlencoded

$POST 内容为:

array( "{"mail":"admin@localhost","password":"xyz"}"=> )

当我使用content-type : application/json

$POST 为空:array()

有没有办法使用 HttpClient 发布 JSON 字符串,或者我应该使用 ArrayList&lt;NameValuePair&gt; 并将我的对象的每个成员添加到实体中?

【问题讨论】:

    标签: json post httpclient


    【解决方案1】:

    我提出了“NameValuePair”解决方案(不在 cmets 中,答案太长),但我认为 StringEntity 能够理解 JSON,参见 How to POST JSON request using Apache HttpClient? 和那里:HTTP POST using JSON in Java

    public String postJSON(String urlToRead,Object o) throws ClientProtocolException, IOException {
        String result="";
        CloseableHttpClient httpClient = HttpClients.createDefault();
        try {
            HttpPost postRequest = new HttpPost(urlToRead);
            postRequest.setHeader("content-type", "application/x-www-form-urlencoded");
    
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    
            //{"mail":"admin@localhost", "password":"xyz"}    
            JsonElement elm= gson.toJsonTree(o);
            JsonObject jsonObj=elm.getAsJsonObject();
            for(Map.Entry<String, JsonElement> entry:jsonObj.entrySet()){
                nameValuePairs.add(new BasicNameValuePair(entry.getKey(),entry.getValue().getAsString()));
            }
             postRequest.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            result = httpClient.execute(postRequest, responseHandler);
        }finally {
            httpClient.close();;
        }
        return result;
    }
    

    这样,$POST 的内容是正确的:array("mail"=&gt;"admin@localhost","password"=&gt;"xyz")

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-19
      • 2019-05-06
      • 1970-01-01
      • 1970-01-01
      • 2019-10-05
      • 1970-01-01
      • 2018-05-01
      • 1970-01-01
      相关资源
      最近更新 更多