[英]Strange response after http POST with JSON in Java
I'm doing what should be a simple http post that includes a json string as the http body. 我正在做的应该是一个简单的http帖子,其中包含一个json字符串作为http正文。
? ?
All looks good except the response itself - when I turn it into a String it comes back looking strange (not text). 除了响应本身,其他所有内容看起来都很不错-当我将其转换为String时,它看起来又很奇怪(不是文本)。 How might I get this in plain text?
我怎么能以纯文本形式得到它? Or what did I do wrong during the post to get this response?
或者在发帖过程中我做错了什么以获得此回复? (note - if I exclude the cookies during the POST I do get plain html back from the server w/ a valid "access denied" message)
(注意-如果我在POST期间排除cookie,则确实会从服务器返回纯html,带有有效的“拒绝访问”消息)
Full code for this solution is below 下面是此解决方案的完整代码
public class BaseHttpService {
public ResponseAndCookies doHttpPostWithUrlWithJson(String url, String key, CookieStore cookies) {
try {
StringEntity se = new StringEntity("{\"filters\":true}");
se.setContentType("text/xml");
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
HttpPost httpost = new HttpPost(url);
httpost.setEntity(se);
httpost.setHeader("Accept", "application/json");
httpost.setHeader("Content-Type", "application/json");
return executeHttpRequest(httpost, cookies);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
public ResponseAndCookies executeHttpRequest(HttpRequestBase http, CookieStore cookieStore) {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
String result = "";
if (cookieStore != null) {
((DefaultHttpClient) httpclient).setCookieStore(cookieStore);
}
try {
response = httpclient.execute(http);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
result = convertStreamToString(instream);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
List<Cookie> cookies = ((DefaultHttpClient) httpclient).getCookieStore().getCookies();
CookieStore postCookieStore = ((DefaultHttpClient) httpclient).getCookieStore();
ResponseAndCookies x = new ResponseAndCookies();
x.setCookies(cookies);
x.setResponse(result);
x.setCookieStore(postCookieStore);
httpclient.getConnectionManager().shutdown();
return x;
}
public static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
You tried to read an UTF-8 String with the platform encoding it seems. 您试图读取带有似乎平台编码的UTF-8字符串。 Find out the Content encoding for the returned response and use that to convert to a string.
找出返回响应的Content编码,然后使用该编码转换为字符串。
Let me know if you want to replace the code above with 2 lines of code. 让我知道是否要用2行代码替换上面的代码。 I got a project for you that already takes care of cookies and converting content.
我为您准备了一个项目,该项目已经处理了cookie和转换内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.