title: HttpURLConnection response is not working
body: <p>In my project I have url 
  like:</p>
  
  <pre><code>localhost:8080/myproject/examples/12
  </code></pre>
  
  <p>It contains Json values..To access this field Need to put access key as Header.</p>
  
  <p>Now, What I have done is that :</p>
  
  <pre><code>private String doHttpUrlConnectionAction(String desiredUrl)
    throws Exception
    {
      URL url = null;
      BufferedReader reader = null;
      StringBuilder stringBuilder;
  
      try
      {
        // create the HttpURLConnection
        url = new URL(desiredUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  
        // just want to do an HTTP GET here
        connection.setRequestMethod("GET");
       // connection.setRequestProperty("Content-Type","application/json");
        connection.setRequestProperty("API-KEY", "value");
  
        // uncomment this if you want to write output to this url
        connection.setDoOutput(true);
  
        // give it 15 seconds to respond
        connection.setReadTimeout(15*1000);
        connection.connect();
  
        // read the output from the server
       // reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  
  
        StringBuilder sb = new StringBuilder();
        BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line;
        while ((line = rd.readLine()) != null) {
            sb.append(line);
        }
        return sb.toString();
  
      }
      catch (Exception e)
      {
        e.printStackTrace();
        throw e;
      }
      finally
      {
        // close the reader; this can throw an exception too, so
        // wrap it in another try/catch block.
        if (reader != null)
        {
          try
          {
            reader.close();
          }
          catch (IOException ioe)
          {
            ioe.printStackTrace();
          }
        }
      }
    }
  </code></pre>
  
  <p>this code returns output :</p>
  
  <pre><code>Response Code : 200
  &lt;table border="0" cellpadding="4" cellspacing="0"&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;status&lt;/th&gt;&lt;th&gt;statusCode&lt;/th&gt;&lt;th&gt;data&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td statusCode="200" status="1"&gt;Array&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
  </code></pre>
  
  <p>But when I am accessing this code in httclient, I am getting value properly..</p>
  
  <pre><code>public String ReadHttpResponse(String url){
        StringBuilder sb= new StringBuilder();
        HttpClient client= new DefaultHttpClient();     
        HttpGet httpget = new HttpGet(url);  
        httpget.addHeader("API-KEY", "value");
        try {
            HttpResponse response = client.execute(httpget);
            StatusLine sl = response.getStatusLine();
            int sc = sl.getStatusCode();
            if (sc==200)
            {
                HttpEntity ent = response.getEntity();
                InputStream inpst = ent.getContent();
                BufferedReader rd= new BufferedReader(new InputStreamReader(inpst));
                String line;
                while ((line=rd.readLine())!=null)
                {
                    sb.append(line);
                }
               // System.out.println(sb.toString());
  
            }
            else
            {
                System.out.println("I didn't  get the response!");
  
  
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }
  </code></pre>
  
  <p>Here,I am getting output properly..</p>
  
  <p>Where is the problem in <code>HttpUrlConnection</code>?? What am I doing wrong here?? I have to use <code>HttpUrlConnection</code>.Please guys help me out..</p>