I need to download and save file. I'm trying to use HTTPBuilder
because it has simple API and supports cookies. I have written following code:
//create new httpBuilder and set cookies
def httpBuilder = ...
def file = ...
def inputStream = httpBuilder.get(uri: urlData.url, contentType: ContentType.BINARY)
FileUtils.copyInputStreamToFile(inputStream)
java.lang.OutOfMemoryError: Java heap space
occurs on line def inputStream = httpBuilder.get...
How can I solve it? HTTPBuilder
. What is the best way to download file with cookies support?Have you tried HttpBuilder GET request with custom response-handling logic:
httpBuilder.get(uri: urlData.url, contentType: ContentType.BINARY) {
resp, inputStream ->
FileUtils.copyInputStreamToFile(inputStream)
}
If HttpBuilder has problems which is strange then you can always use the tried and true Apache HttpClient API which has full cookie support.
HttpGet req = new HttpGet(url);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(req);
// validate response code, etc.
InputStream inputStream = response.getEntity().getContent();
You can add a localContext when executing the request to manage cookies.