gpt4 book ai didi

java - 输入流丢失

转载 作者:行者123 更新时间:2023-11-30 04:12:58 25 4
gpt4 key购买 nike

我有一个名为:ServiceCaller.java的类

此类包含用于调用 Web 服务的方法:

public static Response callService(String strURL, String Token, int timeout, Boolean isPostMethod) {

String error = "";
int statusCode = HttpStatus.SC_INTERNAL_SERVER_ERROR;

HttpURLConnection urlConnection = null;

try
{
URL url = new URL(strURL);

// Allow non trusted ssl certificates
if(strURL.startsWith("https"))
{
TrustManagerManipulator.allowAllSSL();
}

urlConnection = (HttpURLConnection) url.openConnection();

if (isPostMethod) {
urlConnection.setRequestMethod("POST");
}
else {
urlConnection.setRequestMethod("GET");
}

// Allow Inputs
urlConnection.setDoInput(true);

// Allow Outputs
urlConnection.setDoOutput(true);

// Don't use a cached copy.
urlConnection.setUseCaches(false);

urlConnection.setRequestProperty("Connection", "Keep-Alive");

urlConnection.setRequestProperty("Content-Type", "application/json");

urlConnection.setRequestProperty("Token", Helpers.getUTF8Encode(Token));

urlConnection.setConnectTimeout(timeout);

DataOutputStream dos = new DataOutputStream(urlConnection.getOutputStream());
dos.flush();
dos.close();

statusCode = urlConnection.getResponseCode();

Response r = new Response(statusCode, urlConnection.getInputStream(), "No Exception");

return r;

} catch (Exception ex) {

error = ex.getMessage();

if (error != null && !error.equals("") && error.contains("401"))
statusCode = HttpStatus.SC_UNAUTHORIZED;

} finally {

urlConnection.disconnect();

}

return new Response(statusCode, null, error);

}

这是 Response 类:

public static class Response
{
private int statusCode;
private InputStream responseStream;
private String exception;

public int getStatusCode() {
return statusCode;
}

public InputStream getResponseStream() {
return responseStream;
}

public String getExceptionError() {
return exception;
}

public Response(int code, InputStream stream, String strException)
{
this.statusCode = code;
this.responseStream = stream;
this.exception = strException;
}
}

这是我用来测试 ServiceCaller 中的函数的 Test 类:

    public class TestDemo {

private static final String EncriptionKey = "keyValueToUse";

public static void main(String[] args) {

try {

String strURL = "http://...";
String strURL2 = "http://...";
String Token = "iTcakW5...";
int timeout = 120000;
Boolean isPostMethod = true;

ServiceCaller.Response resp = ServiceCaller.CallService(strURL2, Token, timeout, isPostMethod);

InputStream inputStream = resp.getResponseStream();
StringWriter writer = new StringWriter();
IOUtils.copy(inputStream, writer);
String resultJSON = writer.toString();

System.out.println("Status Code: " + resp.getStatusCode());
System.out.println("JSON String:\n" + resultJSON);
System.out.println("Exception: " + resp.getExceptionError());

} catch (Exception e) {

e.printStackTrace();

}
}
}

这是执行之前代码的输出:

<小时/>
Status Code: 200
JSON String:

Exception: No Exception
<小时/>

问题在于,Test 类中返回的 InputString 似乎是空的,因为转换为字符串会返回空字符串,但是如果我在 CallService 函数内执行相同的代码来转换 InputString,则转换会成功,另请注意,状态代码和异常(字符串)已正确返回。

最佳答案

public static Response CallService(String strURL, String Token, int timeout, Boolean isPostMethod) {

HttpURLConnection urlConnection = ...

...

new Response(statusCode, urlConnection.getInputStream(), "No Exception");
}

...中缺少的这段代码可能是最重要的部分。我猜您在返回调用者之前关闭了 HttpURLConnection。您执行此操作的方式可能会有所不同:

  • 您只需在返回之前将其关闭
  • try-catch-finally:您将在finally block 中关闭它。
  • 您正在使用 Java 7 中引入的 try-with-resource 构造。HttpURLConnection 可能会自动关闭。这种情况不太可能发生,因为 HttpURLConnection 没有实现 AutoClosable。

关于java - 输入流丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19168876/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com