Metto a disposizione la soluzione che ho trovato sia per richiesta di tipo post che get:
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
public class Downloader {
static public void PostRequest(String url, int i) throws IOException, InterruptedException {
RequestConfig rc = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build();
CloseableHttpClient client = HttpClients.custom().setDefaultRequestConfig(rc).build();
HttpPost httpPost;
ArrayList<NameValuePair> postParameters;
httpPost = new HttpPost(url);
postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("param1Name", "param1Value"));
postParameters.add(new BasicNameValuePair("param2Name", "param2Value"));
httpPost.setEntity(new UrlEncodedFormEntity(postParameters, "UTF-8"));
CloseableHttpResponse response = client.execute(httpPost);
HttpEntity entity = response.getEntity();
if (entity != null) {
if (response.getEntity().getContentType().getValue().trim().equals("valueOfContent-TypeExpeted")) {
String name = response.getFirstHeader("Content-Disposition").getValue();
String fileName = name.replaceFirst("(?i)^.*filename=\"([^\"]+)\".*$", "$1");
FileOutputStream fos = new FileOutputStream("src/" + fileName);
entity.writeTo(fos);
fos.close();
System.out.println("Download success!");
} else if (!response.getEntity().getContentType().getValue().trim().equals("valueOfContent-TypeExpeted") && i < 10) {
i++;
PostRequest(url, i);
} else if (!response.getEntity().getContentType().getValue().trim().equals("valueOfContent-TypeExpeted") && i == 10) {
System.out.println("Download failed!");
}
}
client.close();
}
static public void GetRequest(String url, int i) throws IOException, InterruptedException {
RequestConfig rc = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build();
CloseableHttpClient client = HttpClients.custom().setDefaultRequestConfig(rc).build();
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("User-Agent", "User-Agent");
CloseableHttpResponse response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
if (entity != null) {
if (response.getEntity().getContentType().getValue().trim().equals("valueOfContent-TypeExpeted")) {
String name = response.getFirstHeader("Content-Disposition").getValue();
String fileName = name.replaceFirst("(?i)^.*filename=\"([^\"]+)\".*$", "$1");
FileOutputStream fos = new FileOutputStream("src/" + fileName);
entity.writeTo(fos);
fos.close();
System.out.println("Download success!");
} else if (response.getEntity().getContentType().getValue().trim().equals("valueOfContent-TypeExpeted") && i < 10) {
i++;
GetRequest(url, i);
} else if (response.getEntity().getContentType().getValue().trim().equals("valueOfContent-TypeExpeted") && i == 10) {
System.out.println("Download failed!");
}
}
client.close();
}
public static void main(String[] args) throws IOException, InterruptedException {
PostRequest("https://ResponseURL" , 1);
GetRequest("http://ResponseURL", 1);
}
Il controllo del content-type è utile poichè se quest'ultimo non è del tipo che ci si aspetta il content-disposition è null e il programma va in crash.
Nel mio caso i parametri da inserire nell'Arraylist<NameValuePair> della richiesta Post li ho trovati nella sezione "dati modulo" del developer tools.
Affinchè funzioni servono i seguenti file jar:
httpclient-4.5.6.jar
httpcore-4.4.10.jar
commons-codec-1.10.jar
commons-logging-1.2.jar
Il codice esegue 10 tentativi di download