为什么java.net.HttpURLConnection响应了身份不明的角色?

时间:2015-11-10 07:19:49

标签: java web-services

我的请求执行程序类将未识别的字符作为响应。请帮我理清这个

import java.net.HttpURLConnection;

        connection = (HttpURLConnection) neturl.openConnection();
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Accept", "application/json");
        connection.setRequestProperty("Accept-Charset", "UTF-8");//ADDED

===这里我做了一些补充和补充;逻辑==

        statusCode = connection.getResponseCode();

===这里我做了一些补充和补充; logics.response代码 200 但响应文本未正确显示==

        if ((statusCode == 200) || (statusCode == 201) || (statusCode == 302)) {

           is = connection.getInputStream();
        }
        else {
            is = connection.getErrorStream();
        }

        BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));

        String output;
        while ((output = br.readLine()) != null) {
            retStr += output;
        }

这是我的回复[retStr]

  

}Umo 6 l &gt; I�׬Y+��ك��b����D"U���/��;J�_�$�B�w���'7�����cHM�����v��,6��R�����/�����|�O��؃�Ԙ��WU�UZ�=�Vg0�2�Y��jTkL����f<F�Q�I��%����,� *:F,��Q��T�K}�N���:gjp�6 ��R�e�ca��2/2�D�Pq��m�G����a��H����P���9��T��~�^�'�Kdk�;��֠�[�&m��%fq�XR[)$łn�7\���Z.M�BµQ<*i�C�2#�(TL��!�;��h�)����n�G��h��s���z:� �tB��0����pr��@�E�P���m)�0G<"��5!˸��/yL��U�V+�F%�!(P�\�Tj"�P5��0c����-��î�j��±�Zr4���)�f��f�^Y��&��tT����X)��9݂lV]�����׽��T1č��P5��,��&+�ũ�9.�.m�5Ǫ�� *��t���|r�M�rL f��c����u�xas #g : On PՁ _ q %a4 E ~$ a sщC%“千$ƍƋb/ 3B%C5 /:I%罺ø贳Qs6G〜O5] O + {0克輀}伏ڸ;يى#7 [ d |!L6V要CE \ GT IA ^ GhHfkZr $ RjNu! [\ U + GR%\ M [N;}ӳ2lׯGKAؼ“Ĵ xBpn6 + / GL FRyBfNڜž킏IJ%MCۉހ N; G7mm)aX'ћl@协泰$ CVF v $ Z 93q H Je k t + ? ilb RAu . * 9B^ ( )

1 个答案:

答案 0 :(得分:4)

我认为你在这里输出gzip流作为输出..尝试下面的代码中的东西。

当您拨打https网址时,您会收到HttpsURLConnection作为回复,而不会处理gzip stream,请尝试以下网址。

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Accept-Encoding", "gzip");
...
InputStream inStream = new GZIPInputStream(conn.getInputStream());