java - 如何在Java中阻止字符串被切断
问题描述
我有一个 java 代码来调用一个返回 JWT 令牌作为响应的 REST API。我向 API 发送一个 GET 调用,它会返回一个 JWT 令牌作为响应。令牌返回正常。但是,我注意到令牌正在以某种方式被修剪。
我在网上尝试了所有东西,但似乎没有什么对我有用。下面是我的代码:
try {
URL url = new URL(proxyService.getProperty("proxy.url") + "/" + sessionToken);
log.logText("Connection URL: " + url, logLevel);
String readLine = null;
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream in = ((URLConnection)conn).getInputStream();
int length = 0;
StringBuffer response = new StringBuffer();
byte[] data1 = new byte[1024];
while (-1 != (length = in.read(data1))) {
response.append(new String(data1, 0, length));
}
log.logText("JSON String Result: " + response.toString(), logLevel);
}
conn.disconnect();
} catch(MalformedURLException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
oauthToken = oauthToken.replaceAll("^\"|\"$", "");
log.logText("OAuth2 Token: " + oauthToken, logLevel);
return oauthToken;
问候,了解更多
解决方案
您没有将响应值分配给任何东西。我假设您应该将其分配给oauthToken变量。
也请在 finally 子句中关闭 InputStream 实例,否则会导致资源泄漏。
推荐阅读
- proxypass - Apache ProxyPass 未将原始 http 标头转发给工作人员
- javascript - 如何在 JS 打字机中停止旋转?
- javascript - 我想从选择的值更改csss中给出的表格的背景颜色
- html - 无限水平滚动图案/固定位置的图像
- react-native - React native 无法通过强制更新或设置状态来呈现页面
- amazon-web-services - AWS 签名版本 2 示例不可重现
- javascript - 如何循环/处理Javascript键:值对?
- linux - 当从 SecureCRT 调用时,脚本退出在线调用 tput
- c++ - 从 64 位应用程序将 32 位图像映射到 32 位进程
- xml - 如果兄弟节点的子节点存在,则父节点输出 XSLT