堆栈交换 oauth 响应不可读
Stack exchange oauth response unreadable
您好,我正在尝试基于 Java 和用于堆栈交换的 apache Oltu 库 api 设置授权 oauth 客户端。
在控制台中打印时,我收到的响应如下所示
u��n��0����"+����tU��l��*k��l⁄��{BưM����s�h�W�#��ˇWj@ٹ�F*P� ����������N��р��=ѹ��\k��
在浏览器中,响应以可读格式很好地打印出来。我的问题与
但正如公认的答案所暗示的那样,响应流是 gzip 压缩的 JSON。当我尝试解压缩响应时,我的程序显示 "Not in GZIP format" 格式。我试过以下:
新 GZIPInputStream(响应)
如果我将响应转换为十六进制,我会得到以下结果:
0x1FEFBFBD080000000000040075EFBFBDEFBFBD6EEFBFBD3010EFBFBD5FEFBFBDEFBFBD222B140CEFBFBDEFBFBDEFBFBD7455EFBFBDEFBFBD6CEFBFBDEFBFBD2A6BEFBFBD07EFBFBD156C13DB84EFBFBDEFBFBD7B0742D4A84DEFBFBDEFBFBDEFBFBDEFBFBD73EFBFBD68EFBFBD57EFBFBD23EFBFBDEFBFBDDA8757EFBFBD086A40D9B9EFBFBD462A50EFBFBDEFBFBDEFBFBDEFBFBDEFBFBD1216EFBFBDEFBFBDEFBFBDEFBFBDEFBFBD3C61EFBFBD1B1505D78441EFBFBDDF95EFBFBDEFBFBDCE8B2627EFBFBDEFBFBD4433EFBFBD6E45EFBFBDEFBFBD610CEFBFBDEFBFBD1142EFBFBDEFBFBD29EFBFBD6B545241EFBFBD44455EEFBFBD4DEFBFBD45797052EFBFBD10EFBFBD69C99B42E49C8BEFBFBD79EFBFBDEFBFBD08513B2BEFBFBD0B58EFBFBDEFBFBD22EFBFBDEFBFBDEFBFBD33EFBFBD3CEFBFBDEFBFBDEFBFBDEFBFBDEFBFBDEFBFBD71365EEFBFBD600BEFBFBDEFBFBD1748EFBFBDEFBFBD0F616DEFBFBDEFBFBDEFBFBDEFBFBDDBB5EFBFBD7321442EEFBFBD66EFBFBD3C07EFBFBD32EFBFBD13EFBFBDEFBFBD6BD0813CEFBFBD6207EFBFBD1756EFBFBDCDA9EFBFBDEFBFBD2DEFBFBD14EFBFBDEFBFBD35EFBFBD6D1BEFBFBD3B66EFBFBD1FEFBFBDEFBFBDEFBFBDEFBFBD1D7A0376EFBFBDEFBFBD515BEFBFBDEFBFBD2E314EEFBFBD4DEFBFBD10EFBFBD7B72EFBFBD 30EFBFBDEFBFBDEFBFBDCE9974EFBFBD0FE9A1BD67474CCD9377EFBFBD1E516A03037E5059EFBFBDEFBFBD76EFBFBDEFBFBD0211EFBFBD2EEFBFBD16EFBFBD45EFBFBDEFBFBDEFBFBDEFBFBD2EEFBFBD38EFBFBD42EFBFBD05EFBFBD725E6156EFBFBDE9B1A8EFBFBDEFBFBD7B38677973EFBFBDEFBFBD5AEFBFBDEFBFBDEFBFBD73EFBFBDC69FEFBFBDEFBFBD68EFBFBDEFBFBD611A61EFBFBD16EFBFBD36E8A38DEFBFBDCF845D20EFBFBD4BEFBFBDEFBFBDEFBFBDEFBFBD797611EFBFBDEFBFBD5FEFBFBD164EEFBFBDEFBFBDD180EFBFBDEFBFBD0EEFBFBD3DD1B9EFBFBD015C1A6BEFBFBDEFBFBD020000
所以我现在的问题是我无法直接读取 JSON 并且无法使用 GZIP 解压缩它。我现在可以做什么来处理数据?有任何想法吗 ?感谢您的时间。
你可以试试OkHttp。这是一个适合我的例子。自动帮我解决了那个解压问题
UriComponents uriComponents =
UriComponentsBuilder.newInstance()
.scheme("https")
.host("api.stackexchange.com")
.path("/2.2/users")
.queryParam("order", "desc")
.queryParam("sort", "reputation")
.queryParam("inname", fullName)
.queryParam("site", "Whosebug")
.build()
.encode();
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(uriComponents.toUriString())
.get()
.addHeader("cache-control", "no-cache")
.build();
Response response = client.newCall(request).execute();
return new JSONObject(response.body().string());
Maven 依赖
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>okhttp</artifactId>
<version>2.7.5</version>
</dependency>
截至目前,我编写了一个像这样的直接 http 调用 // HTTP GET 请求
private String sendGet(String accessToken) 抛出异常 {
String url = "https://api.stackexchange.com/2.2/me?site=Whosebug&key="key"((&access_token=";
url = url + accessToken;
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
int responseCode = con.getResponseCode();
if(responseCode == 200){
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
//System.out.println(con.getContentEncoding());
//System.out.println(con.getContentType());
GZIPInputStream gp = new GZIPInputStream(con.getInputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(gp));
StringBuffer response = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
response.append(line);
}
br.close();
System.out.println(response.toString());
return response.toString();
}
else{
return null;
}
您好,我正在尝试基于 Java 和用于堆栈交换的 apache Oltu 库 api 设置授权 oauth 客户端。
在控制台中打印时,我收到的响应如下所示 u��n��0����"+����tU��l��*k��l⁄��{BưM����s�h�W�#��ˇWj@ٹ�F*P� ����������N��р��=ѹ��\k��
在浏览器中,响应以可读格式很好地打印出来。我的问题与 但正如公认的答案所暗示的那样,响应流是 gzip 压缩的 JSON。当我尝试解压缩响应时,我的程序显示 "Not in GZIP format" 格式。我试过以下: 新 GZIPInputStream(响应)
如果我将响应转换为十六进制,我会得到以下结果:
0x1FEFBFBD080000000000040075EFBFBDEFBFBD6EEFBFBD3010EFBFBD5FEFBFBDEFBFBD222B140CEFBFBDEFBFBDEFBFBD7455EFBFBDEFBFBD6CEFBFBDEFBFBD2A6BEFBFBD07EFBFBD156C13DB84EFBFBDEFBFBD7B0742D4A84DEFBFBDEFBFBDEFBFBDEFBFBD73EFBFBD68EFBFBD57EFBFBD23EFBFBDEFBFBDDA8757EFBFBD086A40D9B9EFBFBD462A50EFBFBDEFBFBDEFBFBDEFBFBDEFBFBD1216EFBFBDEFBFBDEFBFBDEFBFBDEFBFBD3C61EFBFBD1B1505D78441EFBFBDDF95EFBFBDEFBFBDCE8B2627EFBFBDEFBFBD4433EFBFBD6E45EFBFBDEFBFBD610CEFBFBDEFBFBD1142EFBFBDEFBFBD29EFBFBD6B545241EFBFBD44455EEFBFBD4DEFBFBD45797052EFBFBD10EFBFBD69C99B42E49C8BEFBFBD79EFBFBDEFBFBD08513B2BEFBFBD0B58EFBFBDEFBFBD22EFBFBDEFBFBDEFBFBD33EFBFBD3CEFBFBDEFBFBDEFBFBDEFBFBDEFBFBDEFBFBD71365EEFBFBD600BEFBFBDEFBFBD1748EFBFBDEFBFBD0F616DEFBFBDEFBFBDEFBFBDEFBFBDDBB5EFBFBD7321442EEFBFBD66EFBFBD3C07EFBFBD32EFBFBD13EFBFBDEFBFBD6BD0813CEFBFBD6207EFBFBD1756EFBFBDCDA9EFBFBDEFBFBD2DEFBFBD14EFBFBDEFBFBD35EFBFBD6D1BEFBFBD3B66EFBFBD1FEFBFBDEFBFBDEFBFBDEFBFBD1D7A0376EFBFBDEFBFBD515BEFBFBDEFBFBD2E314EEFBFBD4DEFBFBD10EFBFBD7B72EFBFBD 30EFBFBDEFBFBDEFBFBDCE9974EFBFBD0FE9A1BD67474CCD9377EFBFBD1E516A03037E5059EFBFBDEFBFBD76EFBFBDEFBFBD0211EFBFBD2EEFBFBD16EFBFBD45EFBFBDEFBFBDEFBFBDEFBFBD2EEFBFBD38EFBFBD42EFBFBD05EFBFBD725E6156EFBFBDE9B1A8EFBFBDEFBFBD7B38677973EFBFBDEFBFBD5AEFBFBDEFBFBDEFBFBD73EFBFBDC69FEFBFBDEFBFBD68EFBFBDEFBFBD611A61EFBFBD16EFBFBD36E8A38DEFBFBDCF845D20EFBFBD4BEFBFBDEFBFBDEFBFBDEFBFBD797611EFBFBDEFBFBD5FEFBFBD164EEFBFBDEFBFBDD180EFBFBDEFBFBD0EEFBFBD3DD1B9EFBFBD015C1A6BEFBFBDEFBFBD020000
所以我现在的问题是我无法直接读取 JSON 并且无法使用 GZIP 解压缩它。我现在可以做什么来处理数据?有任何想法吗 ?感谢您的时间。
你可以试试OkHttp。这是一个适合我的例子。自动帮我解决了那个解压问题
UriComponents uriComponents =
UriComponentsBuilder.newInstance()
.scheme("https")
.host("api.stackexchange.com")
.path("/2.2/users")
.queryParam("order", "desc")
.queryParam("sort", "reputation")
.queryParam("inname", fullName)
.queryParam("site", "Whosebug")
.build()
.encode();
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(uriComponents.toUriString())
.get()
.addHeader("cache-control", "no-cache")
.build();
Response response = client.newCall(request).execute();
return new JSONObject(response.body().string());
Maven 依赖
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>okhttp</artifactId>
<version>2.7.5</version>
</dependency>
截至目前,我编写了一个像这样的直接 http 调用 // HTTP GET 请求 private String sendGet(String accessToken) 抛出异常 {
String url = "https://api.stackexchange.com/2.2/me?site=Whosebug&key="key"((&access_token=";
url = url + accessToken;
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
int responseCode = con.getResponseCode();
if(responseCode == 200){
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
//System.out.println(con.getContentEncoding());
//System.out.println(con.getContentType());
GZIPInputStream gp = new GZIPInputStream(con.getInputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(gp));
StringBuffer response = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
response.append(line);
}
br.close();
System.out.println(response.toString());
return response.toString();
}
else{
return null;
}