0

There is a billion of topics about Connection, I've tried so many ways to download this file and always fail. When I disable cookies on my web browser I can't download it, for that reason I believe my problem is with cookies. The function of my program is extract the zip, parse the html inside, with Jsoup, insert the content on mysql database and load it on JApplet. Everything is working except the auto-download part, which I have to do manual download in my web browser.

I'm using this class for the cookie, which returns error on

read.CookieManager.storeCookies(CookieManager.java:89)

which corresponds to this line from Cookie class

for (int i=1; (headerName = conn.getHeaderFieldKey(i)) != null; i++) {

and this one from download class

cm.storeCookies(urlConnection);

the download method

    public static void main(String args[]) throws Exception {
    downloadFromUrl("http://www1.caixa.gov.br/loterias/_arquivos/loterias/D_mgsasc.zip", "Mozilla", "C:/", "D_mgsasc.zip", true);
    }

    public static void downloadFromUrl(String srcAddress, String userAgent, String destDir, String destFileName, boolean overwrite) throws Exception
    {
        InputStream is = null;
        FileOutputStream fos = null;

try {
            CookieManager cm = new CookieManager();
            URL url = new URL(srcAddress);
            URLConnection urlConnection = url.openConnection();

            urlConnection.setRequestProperty("User-Agent", userAgent);
            urlConnection.setDoInput(true);
            urlConnection.setDoOutput(true);
            urlConnection.setConnectTimeout(30000);
            urlConnection.setReadTimeout(30000);
            urlConnection.setUseCaches(true);
            urlConnection.connect();
            cm.storeCookies(urlConnection);
            cm.setCookies(url.openConnection()); 

            is = urlConnection.getInputStream();
            fos = new FileOutputStream(destFile);

            byte[] buffer = new byte[1024];

            int len, totBytes = 0;
            while((len = is.read(buffer)) > 0)
            {
                totBytes += len;
                fos.write(buffer, 0, len);
            }
            fos.flush();
            fos.close();

        }
    }

*

updated, removed unnecessary code

*

which returns the following error

java.net.SocketException: Connection reset at zip.DownloadFile.downloadFromUrl(DownloadFile.java:71)

related to this line in code

is = urlConnection.getInputStream();

When I remove cookies set code, the same error of Connection reset persists.

Commentator
  • 640
  • 1
  • 6
  • 22
  • Try to be clean in your code snippets, many people would be too lazy to read whole classes with a dozen lines of meaningful code to the question. – everton Dec 20 '13 at 19:18
  • Are you using the exact same user agent as the one your browser sends? – Robin Green Dec 22 '13 at 09:50
  • Yes. And I used the firefox addon User Agent Switcher 0.7.3 to test if that could be the problem, and the download worked with all of them. – Commentator Dec 23 '13 at 06:02
  • Why are you setting doOutput to true when you aren't doing any output? That converts the request from GET to POST. Is that what you really wanted? – user207421 Jan 05 '14 at 12:20
  • Actually not. But removing that doesn't fix the problem – Commentator Jan 18 '14 at 05:12

0 Answers0