title: Download a file via a proxy java
body: <p>i have a problem downloading a file from a url like <code>www.example.com/example.pdf</code> via a proxy and saving it on the filesystem in java. Does anybody have an Idea on how this could work? if I get the InputStream i can simply save it to filesystem with this: </p>
  
  <pre><code>final ReadableByteChannel rbc = Channels.newChannel(httpUrlConnetion.getInputStream());    
  final FileOutputStream fos = new FileOutputStream(file);
  fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
  fos.close();
  </code></pre>
  
  <p>but how to get the inputstream of the a url via a prox?
  if i am doing it like this:</p>
  
  <pre><code>SocketAddress addr = new InetSocketAddress("my.proxy.com", 8080);
  Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
  URL url = new URL("http://my.real.url.com/");
  URLConnection conn = url.openConnection(proxy);
  </code></pre>
  
  <p>i am getting this exception:</p>
  
  <pre><code>java.net.SocketException: Connection reset
      at java.net.SocketInputStream.read(Unknown Source)
      at java.net.SocketInputStream.read(Unknown Source)
      at java.io.BufferedInputStream.fill(Unknown Source)
      at java.io.BufferedInputStream.read1(Unknown Source)
      at java.io.BufferedInputStream.read(Unknown Source)
      at sun.net.www.http.HttpClient.parseHTTPHeader(Unknown Source)
      at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
      at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
      at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
      at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
      at app.model.mail.crawler.newimpl.FileLoader.getSourceOfSiteViaProxy(FileLoader.java:167)
      at app.model.mail.crawler.newimpl.FileLoader.process(FileLoader.java:220)
      at app.model.mail.crawler.newimpl.FileLoader.run(FileLoader.java:57)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)
  </code></pre>
  
  <p>using this:</p>
  
  <pre><code>final HttpURLConnection httpUrlConnetion = (HttpURLConnection) website.openConnection(proxy);
  httpUrlConnetion.setDoOutput(true);
  httpUrlConnetion.setDoInput(true);
  httpUrlConnetion.setRequestProperty("Content-type", "text/xml");
  httpUrlConnetion.setRequestProperty("Accept", "text/xml, application/xml");
  httpUrlConnetion.setRequestMethod("POST");
  httpUrlConnetion.connect();
  </code></pre>
  
  <p>i am able to download the source of a site which is html, but not a file maybe someone could help me with the properties i have to set for downloading a file.</p>