我有一个名为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似乎是空的,因为转换为string返回一个空字符串但是如果我在CallService函数中转换InputString时使用相同的代码然后转换成功,另请注意,正确返回状态代码和异常(字符串)。
答案 0 :(得分:1)
public static Response CallService(String strURL, String Token, int timeout, Boolean isPostMethod) {
HttpURLConnection urlConnection = ...
...
new Response(statusCode, urlConnection.getInputStream(), "No Exception");
}
......中缺少此代码可能是最重要的部分。我猜你在返回调用者之前关闭了HttpURLConnection。你如何做到这一点可能会有所不同:
return
try-with-resource
构造.HttpURLConnection可能会自动关闭。由于HttpURLConnection没有实现AutoClosable,因此这种可能性更小。答案 1 :(得分:1)
我已经解决了首先从HttpURLConnection获取InputStream,然后将其转换为字节数组,然后将该字节数组放入ByteArrayInputStream
byte[] bytes = IOUtils.toByteArray(urlConnection.getInputStream());
ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes);
return new Response(statusCode, byteStream, "");
根据文档a ByteArrayInputStream:
public ByteArrayInputStream(byte [] buf)创建一个ByteArrayInputStream 所以它使用buf作为缓冲区数组。缓冲区数组不是 复制。 pos的初始值是0和count的初始值 是buf的长度。参数:buf - 输入缓冲区。
答案 2 :(得分:0)
问题是您已经在使用InputStream
方法中的CallService
statusCode = urlConnection.getResponseCode();
Response resp = new Response(statusCode, urlConnection.getInputStream(), "");
InputStream inputStream = resp.getResponseStream();
StringWriter writer = new StringWriter();
IOUtils.copy(inputStream, writer); // consuming the stream
String resultJSON = writer.toString(); // you never use this, so why is it here?
因此,当您尝试在main()
中再次读取时,没有剩余字节。
您只能从中读取一次字节。
这不会抛出任何异常,因为IOUtils
只是调用InputStream#read(...)
,如果已达到EOF,则会-1
返回。
请注意,Java命名约定规定方法名称应以小写字符开头。