httppost不会更改内容类型

时间:2015-10-29 12:24:17

标签: java http-post

这是我的代码:

    $ andle update -p <project_path> [--dryrun] [--remote] [--gradle]

我一直收到错误HttpClient client = HttpClientBuilder.create().build(); HttpPost httpPost = new HttpPost(postUrl); httpPost.setHeader("Content-Type", MediaType.APPLICATION_JSON); StringEntity bodyEntity = new StringEntity("{\"name\":\"hello\"}"); bodyEntity.setContentType(MediaType.APPLICATION_JSON); httpPost.setEntity(bodyEntity); HttpResponse response = client.execute(httpPost); String responseString = new BasicResponseHandler().handleResponse(response); System.out.println(responseString); ,虽然我的网络服务只需要unsupported content type,我检查了httpPost内容类型,它始终是json,为什么它不会更改为JSON?

1 个答案:

答案 0 :(得分:0)

试试这段代码

 public static void HttpPost() {
        try {

            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(
                    "https://api.website.net/auth/token");
            StringEntity input = new StringEntity("{\"name\":\"hello\"}");
            post.setEntity(input);
            HttpResponse response = client.execute(post);
            BufferedReader rd = new BufferedReader(new InputStreamReader(
                    response.getEntity().getContent()));
            String line = "";
            while ((line = rd.readLine()) != null) {
                System.out.println(line);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }