title: Java Load image from URLConnection
body: <p>I have a URLConnection, that access a web page.</p>
  
  <pre><code>URL url = new URL("https://domain");
     con = url.openConnection();
     con.setDoOutput(true);
  </code></pre>
  
  <p>Then i sent some data to the server using <code>con.setRequestProperty()</code></p>
  
  <p>I get the response cookies fro ma specified field using  </p>
  
  <pre><code>String headerValue = con.getHeaderField(6);
  </code></pre>
  
  <p>I also get the html and parse an image url from there. But here is a problem. I can get this image only by sending cache data back to the server ,when i acces my image.</p>
  
  <p>So i open a new connection</p>
  
  <pre><code>URL url1 = new URL("https://domain/image); 
      URLConnection con1 = url1.openConnection();
  </code></pre>
  
  <p>I send the cookies back to the server <code>con1.setRequestProperty("Cookie", headerValue);</code></p>
  
  <p>And finally i try to acces the image using BufferedInputStream and then creating an iamge in a JLabel</p>
  
  <pre><code>BufferedInputStream in = new BufferedInputStream(con1.getInputStream());
         ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
         int c;
         while ((c = in.read()) != -1) {
           byteArrayOut.write(c);
         }
  
  
         Image image = Toolkit.getDefaultToolkit().createImage(
                 byteArrayOut.toByteArray());
  
      label.setIcon(new ImageIcon(image));
  </code></pre>
  
  <p>The problem is this seems to not work. Is it another way to get an file from a server through a URlConnection?
  Error code</p>
  
  <pre><code>Server returned HTTP response code: 400 for URL: https://domain/image
  at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
  at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
  </code></pre>
  
  <p>Thanks in advance</p>