Java httppost没有';不要更改内容类型
这是我的代码:Java httppost没有';不要更改内容类型,java,http-post,Java,Http Post,这是我的代码: HttpClient client = HttpClientBuilder.create().build(); HttpPost httpPost = new HttpPost(postUrl); httpPost.setHeader("Content-Type", MediaType.APPLICATION_JSON); StringEntity bodyEntity = new StringEntity("{\"name\":\"hello\"}"); bodyEntity.
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);
我一直收到错误不支持的内容类型
,虽然我的web服务只接受json
,但我检查了httpPost内容类型,它总是纯文本
,为什么它不改为json请?试试这段代码
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());
}
这怎么能解决我的问题?内容类型在哪里?