Download PDF using Apache HttpClient - java

I'm writing a program to download all of my monthly statements from my ISP using HttpClient. I can login to the site, access pages, and download pages but I can't download my PDF statements. It just downloads some HTML. I used the answer to this question to start with. Here is my method where I'm trying to download the PDF:
public void downloadPdf() throws ClientProtocolException, IOException {
HttpGet httpget = new HttpGet("https://www.cox.com/ibill/PdfBillingStatement.stmt?account13=123&stmtCode=001&cycleDate=7/21/2014&redirectURL=error.cox");
HttpResponse response = client.execute(httpget);
System.out.println("Download response: " + response.getStatusLine());
HttpEntity entity = response.getEntity();
InputStream inputStream = null;
OutputStream outputStream = null;
if (entity != null) {
long len = entity.getContentLength();
inputStream = entity.getContent();
outputStream = new FileOutputStream(new File("/home/bkurczynski/Desktop/statement.pdf"));
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
outputStream.close();
}
}
Any help would be greatly appreciated. Thank you!

HttpClient httpClient = HttpClientBuilder.create().build();
try {
HttpGet request = new HttpGet("https://www.cox.com/ibill/PdfBillingStatement.stmt?account13=123&stmtCode=001&cycleDate=7/21/2014&redirectURL=error.cox");
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
String filePath = "hellow.txt";
FileOutputStream fos = new FileOutputStream(new File(filePath));
int inByte;
while ((inByte = is.read()) != -1)
fos.write(inByte);
is.close();
fos.close();
} catch (Exception ex) {
}

Related

How to download a file from another GET call using Apache Http?

I'm trying to download a file from my local Jasper server using REST API:
http: // : / jasperserver [-pro] / rest_v2 /
reportExecutions / requestID / exports / exportID / outputResource
My interest is that I want to prevent my client from saving a file on the server, I want a direct download using the output from the previous GET call (as a small bridge, nothing more).
I have been using the Apache Http API to do this. Previously I had to make other calls to authenticate, to request the resource and now .... download it.
My problem is that when I download the file, it comes with 0kb and the browser reports that the file is corrupted (it is a pdf that I want to download).
This is the code I'm using to download the file.
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String pass = request.getParameter("pass");
String fileType = "pdf"; // request.getParameter("type") pdf o xls
CloseableHttpClient httpClient = HttpClients.createDefault();
CookieStore cookieStore = new BasicCookieStore();
HttpContext httpContext = new BasicHttpContext();
httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
List<NameValuePair> urlParameters = new ArrayList<>();
urlParameters.add(new BasicNameValuePair("j_username",username));
urlParameters.add(new BasicNameValuePair("j_password",pass));
boolean valid = executeAuthenticationValidation(urlParameters,response,httpClient,httpContext);
if(valid) {
ReportObject repObj = requestJasperReport(request.getParameter("params"),fileType,response,httpClient,httpContext);
if(repObj != null) {
String requestId = repObj.requestId;
String exportId = repObj.exports.get(0).id;
HttpGet get = new HttpGet("http://localhost:8081/jasperserver/rest_v2/reportExecutions/"+requestId+"/exports/"+exportId+"/outputResource");
int rescod;
HttpEntity content;
String name;
String filetype;
try (CloseableHttpResponse chres = httpClient.execute(get,httpContext);) {
StatusLine status = chres.getStatusLine();
rescod = status.getStatusCode();
name = chres.getFirstHeader("Content-Disposition").getValue().split(";")[1];
filetype = chres.getFirstHeader("Content-Type").getValue();
content = chres.getEntity();
}
if(rescod==200) {
response.setContentType(filetype);
response.setHeader("Content-disposition", name);
try (InputStream in = content.getContent();
OutputStream out = response.getOutputStream()) {
byte[] buffer = new byte[1024];
int numBytesRead;
while ((numBytesRead = in.read(buffer)) > 0) {
out.write(buffer, 0, numBytesRead);
}
}
httpClient.close();
}
}
} else {
// Error
}
}
Ok, Lol my bad, this happens when you don't control properly the Try-catch with Resources. Just had to move some code inside of the Try block:
if(repObj != null) {
String requestId = repObj.requestId;
String exportId = repObj.exports.get(0).id;
HttpGet get = new HttpGet("http://localhost:8081/jasperserver/rest_v2/reportExecutions/"+requestId+"/exports/"+exportId+"/outputResource");
HttpEntity content;
String name;
String filetype;
try (CloseableHttpResponse chres = httpClient.execute(get,httpContext);) {
StatusLine status = chres.getStatusLine();
name = chres.getFirstHeader("Content-Disposition").getValue().split(";")[1];
filetype = chres.getFirstHeader("Content-Type").getValue();
content = chres.getEntity();
if(status.getStatusCode()==200) {
response.setContentType(filetype);
response.setHeader("Content-disposition", name);
response.setHeader("Content-Length", String.valueOf(content.getContentLength()));
try (InputStream in = content.getContent();
OutputStream out = response.getOutputStream()) {
byte[] buffer = new byte[1024];
int numBytesRead;
while ((numBytesRead = in.read(buffer)) > 0) {
out.write(buffer, 0, numBytesRead);
}
}
httpClient.close();
}
}
}
} else {
// Error
}
I was getting 0kb because the InputStream was closing it early.

Java download html

I am trying do download the html of a website:
String encoding = "UTF-8";
HttpContext localContext = new BasicHttpContext();
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(MYURL);
httpget.setHeader("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3");
HttpResponse response = httpclient.execute(httpget, localContext);
HttpEntity entity = response.getEntity();
InputStream instream = entity.getContent();
String html = getStringFromInputStream(encoding, instream);
And in the and of the html string i get:
...
21912
0
0
And i don't get the full html,any idea how to fix?
EDIT
private static String getStringFromInputStream(String encoding, InputStream instream) throws UnsupportedEncodingException, IOException {
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(new InputStreamReader(instream, encoding));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} finally {
instream.close();
}
String result = writer.toString();
return result;
}
I would suggest rather use EntityUtils:
HttpEntity entity = response.getEntity();
String html = EntityUtils.toString(entity);
or
HttpEntity entity = response.getEntity();
String html = EntityUtils.toString(entity, encoding);

Upload photo from android

I want to upload the photo from android to server. I made the web service in Jersey Api. But I am getting 415 error when sending the photo.
Please help me to solve this.
I tried complete day..
Android Code:
FileBody bin = new FileBody(file, "image/jpg");
MultipartEntity mp = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
mp.addPart("file", bin);
httpClient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.addHeader("Content-Type", "multipart/form-data");
httppost.setEntity(mp);
HttpResponse response = httpClient.execute(httppost);
if (response.getStatusLine().getStatusCode() == 200) {
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
response.getEntity().writeTo(outstream);
return true;
}
Web service code:
#POST
#Path("uploadphoto")
#Consumes(MediaType.MULTIPART_FORM_DATA)
#Produces("text/plain")
public String uploadNotices(#FormDataParam("file") InputStream picStream) {
try {
OutputStream out = new FileOutputStream(new File("d://1.png"));
int read = 0;
byte[] bytes = new byte[1024];
out = new FileOutputStream(new File("d://1.png"));
while ((read = picStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
return "yes";
}
415 is returned by the server when the entity sent in a request (content in a POST or PUT) has an unsupported mediatype.
Make sure you are sending the same media type which server is asking for.
Why 500 error occured ?? Read this...

generate byte array from StringBuffer.toString

What I'm trying to do is to generate a byte array from a url.
byte[] data = WebServiceClient.download(url);
The url returns json
public static byte[] download(String url) {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
try {
HttpResponse response = client.execute(get);
StatusLine status = response.getStatusLine();
int code = status.getStatusCode();
switch (code) {
case 200:
StringBuffer sb = new StringBuffer();
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
is.close();
sContent = sb.toString();
break;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return sContent.getBytes();
}
This data is used as a parameter for String
String json = new String(data, "UTF-8");
JSONObject obj = new JSONObject(json);
for some reason, I get this error
I/global ( 631): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
I think something there must be missing here sContent = sb.toString(); or here return sContent.getBytes(); but I'm not sure though.
1. Consider using Apache commons-io to read the bytes from InputStream
InputStream is = entity.getContent();
try {
return IOUtils.toByteArray(is);
}finally{
is.close();
}
Currently you're unnecessarily converting the bytes to characters and back.
2. Avoid using String.getBytes() without passing the charset as a parameter. Instead use
String s = ...;
s.getBytes("utf-8")
As a whole I'd rewrite you're method like this:
public static byte[] download(String url) throws IOException {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
HttpResponse response = client.execute(get);
StatusLine status = response.getStatusLine();
int code = status.getStatusCode();
if(code != 200) {
throw new IOException(code+" response received.");
}
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
try {
return IOUtils.toByteArray(is);
}finally{
IOUtils.closeQuietly(is.close());
}
}

Problem with downloading file from server that uses basic http authentication from my java code

I have written the following java code to download a file from a server that uses http basic authentication. But im getting Http 401 error.I can however download the file by hitting the url directly from the browser.
OutputStream out = null;
InputStream in = null;
URLConnection conn = null;
try {
// Get the URL
URL url = new URL("http://username:password#somehost/protected-area/somefile.doc");
// Open an output stream for the destination file locally
out = new BufferedOutputStream(new FileOutputStream("file.doc"));
conn = url.openConnection();
in = conn.getInputStream();
// Get the data
byte[] buffer = new byte[1024];
int numRead;
while ((numRead = in.read(buffer)) != -1) {
out.write(buffer, 0, numRead);
}
} catch (Exception exception) {
exception.printStackTrace();
}
But,im getting the following exception when i run the program :
java.io.IOException: Server returned HTTP response code: 401 for URL: http://username:password#somehost/protected-area/somefile.doc
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1436)
at TestDownload.main(TestDownload.java:17)
I am however able to download the file by hitting the url , http://username:password#somehost/protected-area/somefile.doc, directly from the browser.
What could be causing this problem, and any way to fix it ?
Please Help
Thank You.
I'm using org.apache.http:
private StringBuffer readFromServer(String url) {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() {
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
if (authState.getAuthScheme() == null) {
Credentials credentials = new UsernamePasswordCredentials(
Constants.SERVER_USERNAME,
Constants.SERVER_PASSWORD);
authState.setAuthScheme(new BasicScheme());
authState.setAuthScope(AuthScope.ANY);
authState.setCredentials(credentials);
}
}
};
httpclient.addRequestInterceptor(preemptiveAuth, 0);
HttpGet httpget = new HttpGet(url);
HttpResponse response;
InputStream instream = null;
StringBuffer result = new StringBuffer();
try {
response = httpclient.execute(httpget);
etc...

Categories

Resources