我正试图“解码”来自API的乱码回复。已确认不是gzip编码。
Request:
GET https://<api>/automation-29522e405c/administrator/virtcir_492a439bac-0124-23482186
Cookie: eyJpZGVudGl0eSI6ICJ7XCJyZWFsbVwiOiBcImRlbjE1XCIsIFwidmFsdWVcIjogXCJ7XFxcImN1c3RvbWVyXFxcIjogXFxcInJvb3RcXFwiLCBcXFwidXNlcl9pZFxcXCI6IFxcXCJiNmY1NTZmNi02ZWU4LTQwODUtOGNlNC1iOTI0N2Q1OGY4ODZcXFwiLCBcXFwiZW50aXR5X3R5cGVcXFwiOiBcXFwidXNlclxcXCIsIFxcXCJzZXNzaW9uX2V4cGlyZXNcXFwiOiAxNTE2ODQ4NTI0LjIxOTUwODksIFxcXCJleHBpcmVzXFxcIjogMTUxNjgzOTUyNC4yMTk1NjYxLCBcXFwidXNlclxcXCI6IFxcXCIvcm9vdC9yb290XFxcIiwgXFxcImdyb3Vwc1xcXCI6IFtcXFwiL3Jvb3RcXFwiXSwgXFxcInJlYWxtXFxcIjogXFxcImRlbjE1XFxcIn1cIiwgXCJzaWduYXR1cmVcIjogXCJiNk1YVmZCNFRWdFJ3dzE5a2pYbGdNV3g2UEZyeWRoa3puOUFhdzBlWmRWVis2UE0vSzhIbVp1ekNQMzNFQjY1dnV4c1BGSlpCSVVJRVM2d2JITHVVS2N3a05CQTJVWDJTYVFNNWNQOS93alhXdXo2TUJ3dklLUzYxbVZ5dmVCbHBSVUhUSHp2NTY1anhnMVRDZWtuUUw3aXk2ejdOaWhIWXFpd1VFMVVCS2VLR2oycmJNaDFzQ203K1NtVTRPQitXNXhoZGk0NHBZUExQSDhJbWtxZEc2N1JBb1VMSUlWNkg2ZGkvaFlGTUdWR3BzbCtDYmsrbnRJWTlabXoyMXozNlM5YklkQ2Y5bGhGSjByS1lTbWxnSEtzd1l0NlVUdEFKUFFwR1I0Z0xyTEFKVlBUdkc3OTR0UmVVSjlScFhHTHJrZnoyUGFxU0ptYVdmN2tZaTE0cGc9PVwifSJ9; Path=/; Max-Age=1800
Accept: application/json
Accept-Encoding: gzip;q=1.0, identity; q=0.5
Content-Type: application/compute-v3+json
Response:
200 OK
Server: [nginx]
X-Transaction-Id: [1f307a45-60c7-4005-b371-73af56ecf6f8]
Content-Encoding: [gzip]
Connection: [keep-alive]
Vary: [Accept, Accept-Encoding]
Set-Cookie [eyJpZGVudGl0eSI6ICJ7XCJyZWFsbVwiOiBcImRlbjE1XCIsIFwidmFsdWVcIjogXCJ7XFxcImN1c3RvbWVyXFxcIjogXFxcInJvb3RcXFwiLCBcXFwidXNlcl9pZFxcXCI6IFxcXCJiNmY1NTZmNi02ZWU4LTQwODUtOGNlNC1iOTI0N2Q1OGY4ODZcXFwiLCBcXFwiZW50aXR5X3R5cGVcXFwiOiBcXFwidXNlclxcXCIsIFxcXCJzZXNzaW9uX2V4cGlyZXNcXFwiOiAxNTE2ODQ4NTI0LjIxOTUwODksIFxcXCJleHBpcmVzXFxcIjogMTUxNjgzOTUyNC4yMTk1NjYxLCBcXFwidXNlclxcXCI6IFxcXCIvcm9vdC9yb290XFxcIiwgXFxcImdyb3Vwc1xcXCI6IFtcXFwiL3Jvb3RcXFwiXSwgXFxcInJlYWxtXFxcIjogXFxcImRlbjE1XFxcIn1cIiwgXCJzaWduYXR1cmVcIjogXCJiNk1YVmZCNFRWdFJ3dzE5a2pYbGdNV3g2UEZyeWRoa3puOUFhdzBlWmRWVis2UE0vSzhIbVp1ekNQMzNFQjY1dnV4c1BGSlpCSVVJRVM2d2JITHVVS2N3a05CQTJVWDJTYVFNNWNQOS93alhXdXo2TUJ3dklLUzYxbVZ5dmVCbHBSVUhUSHp2NTY1anhnMVRDZWtuUUw3aXk2ejdOaWhIWXFpd1VFMVVCS2VLR2oycmJNaDFzQ203K1NtVTRPQitXNXhoZGk0NHBZUExQSDhJbWtxZEc2N1JBb1VMSUlWNkg2ZGkvaFlGTUdWR3BzbCtDYmsrbnRJWTlabXoyMXozNlM5YklkQ2Y5bGhGSjByS1lTbWxnSEtzd1l0NlVUdEFKUFFwR1I0Z0xyTEFKVlBUdkc3OTR0UmVVSjlScFhHTHJrZnoyUGFxU0ptYVdmN2tZaTE0cGc9PVwifSJ9;Version=1;Max-Age=1799]
Content-Length: [404]
Date: [Wed, 24 Jan 2018 23:48:44 GMT]
Content-Type: [application/json]
���������QKo�@�/>c�]?��(yȇ��D�!���z0#���������C�H=�:3�k�ß��E�����B�z�4�P���ƙ���I���`�=Y���J iRÔ ï¿½X�4�I:�b��`���;�[D�6�E6���c����u�T
�"M�H7*ڋ3����F;��"�8KC�&aZ`�J�P�b��\��Z�"�+,�Ǯ;�Zv��@|ӂ�O��I���j찧퇺?
�Sc�����Oj�����`u)T���cݑZs
zC,�_&��÷;®qW�K�Z�t�k&�x�ѱ{��i�af�90���r�P>�M�f��q�����Sŋ�2W�2��1��w#mqud�+f}[ݔ�=ÔEqh��|��f��e6 ���-}Z��׌�����
这是我的代码,它接收来自API的响应,检查它是否被gzip压缩并返回一个JSON格式的String。它在 GZIPInputStream zippedInputStream = new GZIPInputStream(inp);
行引发异常public static boolean isZipped(final byte[] compressed) {
return (compressed[0] == (byte) (GZIPInputStream.GZIP_MAGIC)) && (compressed[1] == (byte) (GZIPInputStream.GZIP_MAGIC >> 8));
}
public String getResponseBody(Response response) throws IOException{
String contentEncoding = response.getHeaders().getFirst("Content-Encoding");
writeLog("Value of Content-encoding header " + contentEncoding);
if(contentEncoding != null && contentEncoding.indexOf("gzip") != -1){
writeLog("This is gzipped content " );
try {
StringWriter responseBody = new StringWriter();
PrintWriter responseWriter = new PrintWriter(responseBody);
writeLog("Bytes are zipped = " + isZipped(response.getResponseBody().getBytes()));
ByteArrayInputStream inp = new ByteArrayInputStream(response.getResponseBody().getBytes());
GZIPInputStream zippedInputStream = new GZIPInputStream(inp);
BufferedReader r = new BufferedReader(new InputStreamReader(zippedInputStream, "UTF-8"));
String line = null;
while( (line =r.readLine()) != null){
responseWriter.println(line);
}
writeLog(responseBody.toString());
return responseBody.toString();
} catch (Exception e) {
writeLog(e);
}
}
writeLog("The response is not zipped");
return response.getResponseBody();
}
一些Logging输出。
Value of Content-encoding header gzip
This is gzipped content
Bytes are zipped = false
java.util.zip.ZipException: Not in GZIP format
at java.util.zip.GZIPInputStream.readHeader(GZIPInputStream.java:165)
at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:79)
at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:91)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at oracle.cloud9.qa.Method.run(Method.java:471)
at java.lang.Thread.run(Thread.java:745)
我感觉数据存在但不能理解。我一直在寻找答案,尝试了一堆但没有成功。如果有人可以看一看并指出任何明显的问题并提出建议以便解决这个问题,我将不胜感激。
如果需要,还可以添加更多信息。
有时候,我得到了正确的答案
Request:
GET https://<api>/automation-4110ff83c8/administrator/virtcir_576c0d077f-0124-233002191
Cookie: eyJpZGVudGl0eSI6ICJ7XCJyZWFsbVwiOiBcImRlbjE1XCIsIFwidmFsdWVcIjogXCJ7XFxcImN1c3RvbWVyXFxcIjogXFxcInJvb3RcXFwiLCBcXFwidXNlcl9pZFxcXCI6IFxcXCJiNmY1NTZmNi02ZWU4LTQwODUtOGNlNC1iOTI0N2Q1OGY4ODZcXFwiLCBcXFwiZW50aXR5X3R5cGVcXFwiOiBcXFwidXNlclxcXCIsIFxcXCJzZXNzaW9uX2V4cGlyZXNcXFwiOiAxNTE2ODQ3NDI4LjQzNjY4NzksIFxcXCJleHBpcmVzXFxcIjogMTUxNjgzODQyOC40MzY3MTIsIFxcXCJ1c2VyXFxcIjogXFxcIi9yb290L3Jvb3RcXFwiLCBcXFwiZ3JvdXBzXFxcIjogW1xcXCIvcm9vdFxcXCJdLCBcXFwicmVhbG1cXFwiOiBcXFwiZGVuMTVcXFwifVwiLCBcInNpZ25hdHVyZVwiOiBcImZIeDJsNEw3R3p0TUo0K1dia1JSdXhZeTBpb2JaTWZNQVhNa2ZCVzNrRk5UcktFOVJGMXYxOW52WHBnS3I3QnZ1SDY1NFNqU0EzRCs4WFllTkpYbk5jSDFaSEdYb3FmRmp4VG9NeGtlSU5zajJJSUdweTBUdDhnaWkya2txMUVNZnd3MThkQnRqY0phYVZsQkcyUlZEUm13cEVmMGxjSDYvakRRTmxtRzFTRHVidmpBd3VmMWJ3eFhhRkZwc1FkR0IvWHFKaWNBeFhBNmZUVkU5ODNzNThDeFVZbjFPWCtMaXd0Mnlqd3p0TlhCaTkxaVJabzQ1WUJReXZmTzVzcEtocDlnWFRsMjMxN1ZxUDFmc0ozUzBMUHJ2bUxoOWhITHdIY0N2SFpiaWJtQzZrYVpkVjdlUksxTlBCekt0WU44c1pjL1pVUHkvM3JBd28wblBvSmFFQT09XCJ9In0=; Path=/; Max-Age=1800
Accept: application/json
Accept-Encoding: gzip;q=1.0, identity; q=0.5
Content-Type: application/compute-v3+json
Response:
200 OK
Server: [nginx]
X-Transaction-Id: [b0a42947-975d-4c80-9564-f6ccfefc4321]
Connection: [keep-alive]
Vary: [Accept]
Set-Cookie:[eyJpZGVudGl0eSI6IntcInJlYWxtXCI6XCJkZW4xNVwiLFwidmFsdWVcIjpcIntcXFwicmVhbG1cXFwiOlxcXCJkZW4xNVxcXCIsXFxcImN1c3RvbWVyXFxcIjpcXFwicm9vdFxcXCIsXFxcImV4cGlyZXNcXFwiOjEuNTE2ODM4NDI4NDM2NzEyRTksXFxcInVzZXJcXFwiOlxcXCIvcm9vdC9yb290XFxcIixcXFwiZ3JvdXBzXFxcIjpbXFxcIi9yb290XFxcIl0sXFxcImludGVybmFsXFxcIjpmYWxzZSxcXFwidXNlcl9pZFxcXCI6XFxcImI2ZjU1NmY2LTZlZTgtNDA4NS04Y2U0LWI5MjQ3ZDU4Zjg4NlxcXCIsXFxcImVudGl0eV90eXBlXFxcIjpcXFwidXNlclxcXCIsXFxcInNlc3Npb25fZXhwaXJlc1xcXCI6MS41MTY4NDc0Mjg0MzY2ODhFOX1cIixcInNpZ25hdHVyZVwiOlwiTUlpdVhIRzZIS1lmdEYwL2JVbU5rNmROVi8vem0yakVyU3R4ZUo4WkYwdVhpeHc2NXBWM3hMcjFTcTRBeE8rMkYvY2NyT2htQTluZlFqVCtodklLKzNNNjFnSXZNKy8wbGtjTnQ1VW10bzFnc0hVWjhVOTRrTGJPNEtoSHozUG1Ud3ZCUjkxdjR3SHpselVYejZnTjEvNFNMekZuWWZnYmV6VSsvL2pKTzlORFd5SGh2VmdiTGFyRFpYSkxtb29uVStjd3dSQU0xNnRBVWE3ejU3YnE5VzA3TVovRE1LMHNSamFmV0N3OVY2WTFWM21NeXhyY1N2YW8zbnl4VzR2blRVTU9rZFRRcTB6RExEZml4U1lmTFRIR1V5bW5JU3ZRUmdBMDUwUUU4RXV3VUVwUGFTcnRrdzhKdnJPQTVEVE1IajNyQkozRkRXOEtnbGdMUEhxQjRRPT1cIn0ifQ==;Version=1;Max-Age=1799]
Content-Length: [714]
Date: [Wed, 24 Jan 2018 23:30:28 GMT]
Content-Type: [application/json]
{"name":"/automation-4110ff83c8/administrator/virtcir_576c0d077f-0124-233002191","uri":"https://<api>/automation-4110ff83c8/administrator/virtcir_576c0d077f-0124-233002191","id":"991ba85a-29a7-40e9-8393-3b0463947299","referenceId":null,"region":"den15","gatewayId":"/automation-4110ff83c8/administrator/pgw_576c0d077f-0124-233002191","providerId":null,"type":"PUBLIC","bandwidthShapeName":"1Gbps","publicPrefixes":[],"flexFields":null,"orBgpAsn":7160,"customerBgpAsn":0,"providerState":"ACTIVE","bgpSessionState":"DOWN","lifeCycleState":"FAILED","created":1516836622525,"modified":1516836628338}
答案 0 :(得分:0)
我修复它的方法是,在收到响应的代码中注释掉这一行。
setHeader("Accept-Encoding", "gzip;q=1.0, identity; q=0.5")
希望这可以帮助那些面临这个问题的人。