简体   繁体   中英

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.

? 在此处输入图片说明

All looks good except the response itself - when I turn it into a String it comes back looking strange (not text). 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)

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. Find out the Content encoding for the returned response and use that to convert to a string.

Let me know if you want to replace the code above with 2 lines of code. I got a project for you that already takes care of cookies and converting content.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM