title: InputStream being lost
body: <p>I have a class titled: ServiceCaller.java</p>
  
  <p>This class contains a method used to call web services:</p>
  
  <pre><code>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 &amp;&amp; !error.equals("") &amp;&amp; error.contains("401"))
              statusCode = HttpStatus.SC_UNAUTHORIZED;
  
      } finally {
  
          urlConnection.disconnect();
  
      }
  
      return new Response(statusCode, null, error);
  
  }
  </code></pre>
  
  <p>Here's the Response class:</p>
  
  <pre><code>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;
      }
  }
  </code></pre>
  
  <p>This is the Test class that I use to test the function in ServiceCaller:</p>
  
  <pre><code>    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();
  
              }
          }
      }
  </code></pre>
  
  <p>Here's the Output of executing hte previous code:</p>
  
  <hr>
  
  <pre><code>Status Code: 200
  JSON String:
  
  Exception: No Exception
  </code></pre>
  
  <hr>
  
  <p>Here's the problem, the InputString that is returned in the Test class appears to be empty because the conversion to string returns an empty string BUT if I do the same code to convert the InputString inside the CallService function then the conversion is successful, also note that the Status Code and Exception (strings) are being returned correctly.</p>