Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion src/org/pixmob/httpclient/HttpRequestBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public final class HttpRequestBuilder {
private Map<String, List<String>> headers;
private Map<String, String> parameters;
private byte[] content;
private String contentFilePath;
private boolean contentSet;
private String contentType;
private HttpResponseHandler handler;
Expand Down Expand Up @@ -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<String, String> cookies) {
this.cookies = cookies;
return this;
Expand Down Expand Up @@ -192,6 +202,7 @@ public HttpRequestBuilder to(OutputStream output) {
return this;
}

@TargetApi(19)
public HttpResponse execute() throws HttpClientException {
HttpURLConnection conn = null;
UncloseableInputStream payloadStream = null;
Expand Down Expand Up @@ -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);
}
}
Expand Down
23 changes: 23 additions & 0 deletions src/org/pixmob/httpclient/HttpResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down