1

我有一个名为: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);

}

这是响应类:

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 则转换成功,还要注意正确返回状态代码和异常(字符串)。

4

3 回答 3

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-catch-finally:你在 finally 块中关闭它。
  • 您正在使用try-with-resourceJava 7 中引入的构造。 HttpURLConnection 可能会自动关闭。这不太可能,因为 HttpURLConnection 没有实现 AutoClosable。
于 2013-10-03T20:57:00.057 回答
1

我首先从 HttpURLConnection 获取 InputStream,然后将其转换为字节数组,然后将该字节数组放入 ByteArrayInputStream

byte[] bytes = IOUtils.toByteArray(urlConnection.getInputStream());
ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes);

return new Response(statusCode, byteStream, "");

根据文档 ByteArrayInputStream:

public ByteArrayInputStream(byte[] buf) 创建一个 ByteArrayInputStream 以便它使用 buf 作为它的缓冲区数组。不复制缓冲区数组。pos的初值为0,count的初值为buf的长度。参数: buf - 输入缓冲区。

于 2013-10-04T18:07:47.450 回答
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()没有剩余字节了。

您只能从中读取字节一次。

这不会引发任何异常,因为如果已达到 EOF ,IOUtils只需调用InputStream#read(...)返回。-1


请注意,Java 命名约定规定方法名称应以小写字符开头。

于 2013-10-04T13:35:41.757 回答