* support setting request properties for post requests (required for PushBullet support in AMC script)

This commit is contained in:
Reinhard Pointner 2014-03-21 16:32:51 +00:00
parent 1ae64235f1
commit b365bf48c3
2 changed files with 14 additions and 6 deletions

View File

@ -84,9 +84,9 @@ String.metaClass.getHtml = { new XmlParser(new org.cyberneko.html.parsers.SAXPar
String.metaClass.getXml = { new XmlParser().parseText(delegate) }
URL.metaClass.get = { delegate.getText() }
URL.metaClass.post = { Map parameters -> post(delegate.openConnection(), parameters) }
URL.metaClass.post = { byte[] data, contentType = 'application/octet-stream' -> post(delegate.openConnection(), data, contentType) }
URL.metaClass.post = { String text, contentType = 'text/plain', csn = 'utf-8' -> delegate.post(text.getBytes(csn), contentType) }
URL.metaClass.post = { Map parameters, requestParameters = null -> post(delegate, parameters, requestParameters) }
URL.metaClass.post = { byte[] data, contentType = 'application/octet-stream', requestParameters = null -> post(delegate, data, contentType, requestParameters) }
URL.metaClass.post = { String text, contentType = 'text/plain', csn = 'utf-8', requestParameters = null -> post(delegate, text.getBytes(csn), contentType, requestParameters) }
ByteBuffer.metaClass.saveAs = { f -> f = f as File; f = f.absoluteFile; f.parentFile.mkdirs(); writeFile(delegate.duplicate(), f); f }
URL.metaClass.saveAs = { f -> fetch(delegate).saveAs(f) }

View File

@ -173,16 +173,24 @@ public final class WebRequest {
return buffer.getByteBuffer();
}
public static ByteBuffer post(HttpURLConnection connection, Map<String, ?> parameters) throws IOException {
return post(connection, encodeParameters(parameters, true).getBytes("UTF-8"), "application/x-www-form-urlencoded");
public static ByteBuffer post(URL url, Map<String, ?> parameters, Map<String, String> requestParameters) throws IOException {
return post(url, encodeParameters(parameters, true).getBytes("UTF-8"), "application/x-www-form-urlencoded", requestParameters);
}
public static ByteBuffer post(HttpURLConnection connection, byte[] postData, String contentType) throws IOException {
public static ByteBuffer post(URL url, byte[] postData, String contentType, Map<String, String> requestParameters) throws IOException {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.addRequestProperty("Content-Length", String.valueOf(postData.length));
connection.addRequestProperty("Content-Type", contentType);
connection.setRequestMethod("POST");
connection.setDoOutput(true);
if (requestParameters != null) {
for (Entry<String, String> parameter : requestParameters.entrySet()) {
connection.addRequestProperty(parameter.getKey(), parameter.getValue());
}
}
// write post data
OutputStream out = connection.getOutputStream();
out.write(postData);