繁体   English   中英

为什么java.net.HttpURLConnection响应不明字符?

[英]why java.net.HttpURLConnection responsed unidentified characters?

我的请求执行程序类提供了未识别的字符作为响应。 请帮助我解决这个问题

导入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();

===这里我做了一些加法和逻辑。响应代码为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 >.I 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\\ ZM 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 r M rLf c u xas #g : O 为nPՁ_答疑A4E〜$asщC%“千$ƍƋ?B /!3? ? b %c5 ? / :i. %。罺.o。贳.Q Q s.6G。〜o.5 0 + {0克輀}伏ڸ;?!يى#7 [d | L6V要CE \\ ?!GT IA ^ GhHfkZr $ RJ努[\\ U + GR%\\ M [N; } ӳ 2 l ׯgKa ؼ''j xBpn6 + / G L F R yBf N Zڜ i J %M Cۉހ n ; G 7m m; )一个?X'ћl@ $的GTi $ CVfv Z93qߚHJekt + ? ilb RAu . * 9B^ ( ) ?

我认为您在这里得到gzip流作为输出..尝试在代码中进行以下操作。

当您呼叫https URL时,会得到HttpsURLConnection作为返回值,它不处理gzip流,因此请尝试以下操作。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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