multipart request support
This commit is contained in:
1 parent
ca7950004b
commit
7b6b9056d3
1 file changed
+48
-7
@@ -26,6 +26,7 @@ class Request
|
||||
private String payload = null;
|
||||
private HttpURLConnection connection;
|
||||
private String charset;
|
||||
private byte[] bytePayload = null;
|
||||
|
||||
/**
|
||||
* Creates a new Http Request
|
||||
@@ -77,7 +78,7 @@ class Request
|
||||
addHeaders(connection);
|
||||
if (verb.equals(Verb.PUT) || verb.equals(Verb.POST))
|
||||
{
|
||||
addBody(connection, getBodyContents());
|
||||
addBody(connection, getByteBodyContents());
|
||||
}
|
||||
return new Response(connection);
|
||||
}
|
||||
@@ -88,13 +89,11 @@ class Request
|
||||
conn.setRequestProperty(key, headers.get(key));
|
||||
}
|
||||
|
||||
void addBody(HttpURLConnection conn, String content) throws IOException
|
||||
void addBody(HttpURLConnection conn, byte[] content) throws IOException
|
||||
{
|
||||
if (this.charset == null)
|
||||
this.charset = Charset.defaultCharset().name();
|
||||
conn.setRequestProperty(CONTENT_LENGTH, String.valueOf(content.getBytes(charset).length));
|
||||
conn.setRequestProperty(CONTENT_LENGTH, String.valueOf(content.length));
|
||||
conn.setDoOutput(true);
|
||||
conn.getOutputStream().write(content.getBytes(charset));
|
||||
conn.getOutputStream().write(content);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -145,6 +144,16 @@ class Request
|
||||
this.payload = payload;
|
||||
}
|
||||
|
||||
/**
|
||||
* Overloaded version for byte arrays
|
||||
*
|
||||
* @param payload
|
||||
*/
|
||||
public void addPayload(byte[] payload)
|
||||
{
|
||||
this.bytePayload = payload;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a {@link Map} of the query string parameters.
|
||||
*
|
||||
@@ -201,10 +210,32 @@ class Request
|
||||
* Returns the body of the request
|
||||
*
|
||||
* @return form encoded string
|
||||
* @throws OAuthException if the charset chosen is not supported
|
||||
*/
|
||||
public String getBodyContents()
|
||||
{
|
||||
return (payload != null) ? payload : URLUtils.formURLEncodeMap(bodyParams);
|
||||
try
|
||||
{
|
||||
return new String(getByteBodyContents(),getCharset());
|
||||
}
|
||||
catch(UnsupportedEncodingException uee)
|
||||
{
|
||||
throw new OAuthException("Unsupported Charset: "+charset, uee);
|
||||
}
|
||||
}
|
||||
|
||||
byte[] getByteBodyContents()
|
||||
{
|
||||
if (bytePayload != null) return bytePayload;
|
||||
String body = (payload != null) ? payload : URLUtils.formURLEncodeMap(bodyParams);
|
||||
try
|
||||
{
|
||||
return body.getBytes(getCharset());
|
||||
}
|
||||
catch(UnsupportedEncodingException uee)
|
||||
{
|
||||
throw new OAuthException("Unsupported Charset: "+getCharset(), uee);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -227,6 +258,16 @@ class Request
|
||||
return headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the connection charset. Defaults to {@link Charset} defaultCharset if not set
|
||||
*
|
||||
* @return charset
|
||||
*/
|
||||
public String getCharset()
|
||||
{
|
||||
return charset == null ? Charset.defaultCharset().name() : charset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the connect timeout for the underlying {@link HttpURLConnection}
|
||||
*
|
||||
|
||||
Reference in new issue
Block a user