From 8160e0f65baff2776c5e3135912d07f133fe394f Mon Sep 17 00:00:00 2001 From: Asif Mujteba Date: Mon, 14 Jul 2014 16:15:43 +0500 Subject: [PATCH] Provide HTTP Request Content Body From File - Made HTTPRequestBuilder accept body content from a specified file. This would be helpful when making request like PUT with large data. - getResponseBody() method added to HTTPResponse class for extracting all response body text at once. --- .../pixmob/httpclient/HttpRequestBuilder.java | 40 ++++++++++++++++++- src/org/pixmob/httpclient/HttpResponse.java | 23 +++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/src/org/pixmob/httpclient/HttpRequestBuilder.java b/src/org/pixmob/httpclient/HttpRequestBuilder.java index 765a5f9b..07e3752a 100644 --- a/src/org/pixmob/httpclient/HttpRequestBuilder.java +++ b/src/org/pixmob/httpclient/HttpRequestBuilder.java @@ -77,6 +77,7 @@ public final class HttpRequestBuilder { private Map> headers; private Map parameters; private byte[] content; + private String contentFilePath; private boolean contentSet; private String contentType; private HttpResponseHandler handler; @@ -115,6 +116,15 @@ public HttpRequestBuilder content(byte[] content, String contentType) { return this; } + public HttpRequestBuilder contentFromFile(String filePath, String contentType) { + this.contentFilePath = filePath; + this.contentType = contentType; + if (contentFilePath != null) { + contentSet = true; + } + return this; + } + public HttpRequestBuilder cookies(Map cookies) { this.cookies = cookies; return this; @@ -192,6 +202,7 @@ public HttpRequestBuilder to(OutputStream output) { return this; } + @TargetApi(19) public HttpResponse execute() throws HttpClientException { HttpURLConnection conn = null; UncloseableInputStream payloadStream = null; @@ -285,7 +296,34 @@ public HttpResponse execute() throws HttpClientException { final OutputStream out = conn.getOutputStream(); out.write(content); out.flush(); - } else { + } else if (contentFilePath != null) { + conn.setDoOutput(true); + if (!contentSet) { + conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=" + + CONTENT_CHARSET); + } else if (contentType != null) { + conn.setRequestProperty("Content-Type", contentType); + } + File file = new File(contentFilePath); + if (file.exists() && !file.isDirectory()) { + if (Build.VERSION.SDK_INT > 19) { + conn.setFixedLengthStreamingMode(file.length()); + } + else { + conn.setFixedLengthStreamingMode((int) file.length()); + } + + FileInputStream fileInputStream = new FileInputStream(file); + final OutputStream out = conn.getOutputStream(); + + int len = 0; + while ((len = fileInputStream.read(buffer)) != -1) { + out.write(buffer, 0, len); + } + + out.flush(); + fileInputStream.close(); + } else { conn.setFixedLengthStreamingMode(0); } } diff --git a/src/org/pixmob/httpclient/HttpResponse.java b/src/org/pixmob/httpclient/HttpResponse.java index b289b85e..1243be5b 100644 --- a/src/org/pixmob/httpclient/HttpResponse.java +++ b/src/org/pixmob/httpclient/HttpResponse.java @@ -116,6 +116,29 @@ void preload(File temp) throws IOException { payload = new FileInputStream(temp); } + public String getResponseBody() throws IOException { + String enc = getContentCharset(); + if (enc == null) { + enc = "UTF-8"; + } + + InputStream input = getPayload(); + InputStreamReader inputStreamReader = new InputStreamReader(input, enc); + BufferedReader bufferedReader = new BufferedReader(inputStreamReader); + + StringBuilder stringBuilder = new StringBuilder(); + String bufferedStrChunk = null; + + while((bufferedStrChunk = bufferedReader.readLine()) != null){ + stringBuilder.append(bufferedStrChunk); + } + + if (bufferedReader != null) { bufferedReader.close(); } + if (inputStreamReader != null) { inputStreamReader.close(); } + + return stringBuilder.toString(); + } + public void read(StringBuilder buffer) throws IOException { String enc = getContentCharset(); if (enc == null) {