title: Google App Engine - HttpClient execute method closes connection without getting data
body: <h2>The Problem</h2>
  <p>I am using Apache HttpClient in a web servlet hosted on google app engine. The problem i have is that if i visit the url in a web browser, i get a good <a href="https://doctorbase.com/api/dr/search_rest?location=Sacramento,%20California" rel="nofollow noreferrer">JSON result</a>, but if i try to get this same JSON data in JAVA my connection is immediatly closed with no returned entity. Here is the code from my servlet:</p>
  <pre><code>try {
      // Pooling code from: http://stackoverflow.com/a/13763608
      PoolingClientConnectionManager cxMgr = new PoolingClientConnectionManager(
              SchemeRegistryFactory.createDefault());
      cxMgr.setMaxTotal(100);
      cxMgr.setDefaultMaxPerRoute(20);
  
      HttpClient client = new DefaultHttpClient(cxMgr);
      URIBuilder uriQuery = new URIBuilder();
  
      uriQuery.setScheme(&quot;https&quot;);
      uriQuery.setHost(&quot;doctorbase.com&quot;);
      uriQuery.setPath(&quot;/api/dr/search_rest&quot;);
      uriQuery.setParameter(&quot;location&quot;, user.getLocation().getName());
      URI uri = uriQuery.build();
  
      System.out.println(&quot;Setting up get: &quot; + uri.toString());
      HttpGet getRequest = new HttpGet(uri);
      System.out.println(&quot;Calling execute&quot;);
      HttpResponse docApiResponse = client.execute(getRequest);
      System.out.println(&quot;Getting Entity&quot;);
      HttpEntity entity = docApiResponse.getEntity();
  } catch (Exception e) {
      System.out.println(&quot;Exception getting doctors: &quot;
              + e.toString());
      e.printStackTrace();
  }
  </code></pre>
  <p>And here is the log i see in Google App Engine (it appears that the client.Execute(getRequest) line is popping out of the method which shouldnt happen since i have it in a try-catch block and the catch is not executed:</p>
  <pre><code>2013-03-04 19:44:54.593 [s~mybloodpressurelog/1.365731215144292261].&lt;stdout&gt;: Calling getNearbyDoctorInfo
  I 2013-03-04 19:44:54.747 [s~mybloodpressurelog/1.365731215144292261].&lt;stdout&gt;: Setting up get: https://doctorbase.com/api/dr/search_rest?location=Sacramento%2C+California
  I 2013-03-04 19:44:54.747 [s~mybloodpressurelog/1.365731215144292261].&lt;stdout&gt;: Calling execute
  D 2013-03-04 19:44:54.752 org.apache.http.impl.conn.PoolingClientConnectionManager requestConnection: Connection request: [route: {s}-&gt;https://doctorbase.com][total kept alive:
  D 2013-03-04 19:44:54.753 org.apache.http.impl.conn.PoolingClientConnectionManager leaseConnection: Connection leased: [id: 1][route: {s}-&gt;https://doctorbase.com][total kept al
  D 2013-03-04 19:44:54.758 org.apache.http.impl.conn.DefaultClientConnection shutdown: Connection org.apache.http.impl.conn.DefaultClientConnection@f0850b shut down
  D 2013-03-04 19:44:54.758 org.apache.http.impl.conn.DefaultClientConnection close: Connection org.apache.http.impl.conn.DefaultClientConnection@f0850b closed
  D 2013-03-04 19:44:54.758 org.apache.http.impl.conn.PoolingClientConnectionManager releaseConnection: Connection released: [id: 1][route: {s}-&gt;https://doctorbase.com][total kep
  </code></pre>
  <p>Any help solving the mystery is the immediately closing connection is appreciated.</p>
  <h2>Working code</h2>
  <p>(thanks Fahad for pointing me in the right direction!):</p>
  <pre><code>URL doctorUrl = new URL(
      &quot;https://doctorbase.com/api/dr/search_rest?&quot;
              + user.getLocation().getName()
                      .replaceAll(&quot; &quot;, &quot;%2C+&quot;));
  try {
      System.out.println(&quot;IS to String&quot;);
      BufferedReader jsonReader = new BufferedReader(
              new InputStreamReader(doctorUrl.openStream()));
      String json = jsonReader.readLine();
  
      System.out.println(&quot;Doctor JSON: &quot; + json);
  
      DoctorApiResult drs = new Gson().fromJson(json,
              DoctorApiResult.class);
  
      System.out.println(&quot;Deserialized Doctors &quot;);
  
      int numberOfDoctors = drs.getDrs().size();
      System.out.println(&quot;Number of doctors found: &quot;
              + numberOfDoctors);
  
      DoctorBean[] doctors = new DoctorBean[numberOfDoctors];
      drs.getDrs().toArray(doctors);
  
      request.setAttribute(&quot;drs&quot;, doctors);
  } catch (Exception e) {
      System.out
              .println(&quot;Exception getting doctors: &quot; + e.toString());
      e.printStackTrace();
      request.setAttribute(&quot;error&quot;, e.toString());
  }
  </code></pre>