[英]Problem sending HTTP post request in Java
Hey, I'm trying to post an http request with xml body. 嘿,我正在尝试用xml正文发布一个http请求。 This is my code:
这是我的代码:
public void setBla(String url, String cookie, SomeEnum status) {
String availabilityUrl = url;
HttpPost httpPost = new HttpPost(availabilityUrl + VERSION_TEXT);
httpPost.setHeader(HttpHeaders.AUTHORIZATION, LP_AUTH_HEADER_VALUE);
httpPost.setHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_XML);
httpPost.setHeader("X-HTTP-Method-Override", HttpMethod.PUT);
httpPost.setHeader("Cookie", cookie);
httpPost.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML);
HttpResponse response = null;
try {
HttpEntity entity = new StringEntity("<availability><chat>" + status + "</chat></availability>", HTTP.UTF_8);
httpPost.setEntity(entity);
response = m_httpClient.execute(httpPost);
}
catch (Exception e) {
e.printStackTrace();
}
}
Im getting a bad request from the AppServer (400), and in debug I can see that HttpEntity also has ContentType header and its set to text/plain... Why? 我从AppServer(400)得到了一个错误的请求,并且在调试中我可以看到HttpEntity也有ContentType标头并且它设置为text / plain ...为什么? What am I doing wrong?
我究竟做错了什么? What is the right way to send a request with a body?
向身体发送请求的正确方法是什么?
SOLUTION: 解:
OK.. I got it.. Thanks skaffman, that was pretty close :) AbstractHttpEntity declare setContentType function... 好的..我明白了..谢谢skaffman,那是非常接近:) AbstractHttpEntity声明setContentType函数...
Sorry for the bother.. 对不起打扰..
Udi 乌迪
Assuming this is HttpClient 4.1, then you're not using the appropriate constructor for StringEntity
, you probably need to specify the mime type as well as the charset, eg 假设这是HttpClient 4.1,那么你没有为
StringEntity
使用适当的构造StringEntity
,你可能需要指定mime类型以及charset,例如
HttpEntity entity = new StringEntity(xmlString, "application/xml", HTTP.UTF_8);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.