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.