Mercurial Hosting > luan
changeset 727:d6a191618c60
minor
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 10 Jun 2016 14:55:26 -0600 |
parents | 14f136a4641f |
children | 11839152caef |
files | core/src/luan/modules/UrlCall.java core/src/luan/modules/url/UrlCall.java |
diffstat | 2 files changed, 84 insertions(+), 81 deletions(-) [+] |
line wrap: on
line diff
diff -r 14f136a4641f -r d6a191618c60 core/src/luan/modules/UrlCall.java --- a/core/src/luan/modules/UrlCall.java Thu Jun 09 18:43:32 2016 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,81 +0,0 @@ -package luan.modules; - -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.io.Reader; -import java.io.IOException; -import java.net.URLConnection; -import java.net.HttpURLConnection; -import java.net.URL; -import java.util.Map; -import java.util.HashMap; - - -public final class UrlCall { - public final URLConnection connection; - - public UrlCall(String url) throws IOException { - this(new URL(url)); - } - - public UrlCall(URL url) throws IOException { - connection = url.openConnection(); - } - - public void acceptJson() { - connection.setRequestProperty("accept","application/json"); - } - - public String get() throws IOException { - Reader in = new InputStreamReader(connection.getInputStream()); - String rtn = Utils.readAll(in); - in.close(); - return rtn; - } - - public String post(String content,String contentType) throws IOException { - HttpURLConnection connection = (HttpURLConnection)this.connection; - - connection.setRequestProperty("Content-type",contentType); - connection.setDoOutput(true); - connection.setRequestMethod("POST"); - - byte[] post = content.getBytes(); - connection.setRequestProperty("Content-Length",Integer.toString(post.length)); - OutputStream out = connection.getOutputStream(); - out.write(post); - out.flush(); - - Reader in; - try { - in = new InputStreamReader(connection.getInputStream()); - } catch(IOException e) { - InputStream is = connection.getErrorStream(); - if( is == null ) - throw e; - in = new InputStreamReader(is); - String msg = Utils.readAll(in); - in.close(); - throw new UrlCallException(msg,e); - } - String rtn = Utils.readAll(in); - in.close(); - out.close(); - return rtn; - } - - public String post(String content) throws IOException { - return post(content,"application/x-www-form-urlencoded"); - } - - public String postJson(String content) throws IOException { - return post(content,"application/json"); - } - - public static final class UrlCallException extends IOException { - UrlCallException(String msg,IOException e) { - super(msg,e); - } - } -}
diff -r 14f136a4641f -r d6a191618c60 core/src/luan/modules/url/UrlCall.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/core/src/luan/modules/url/UrlCall.java Fri Jun 10 14:55:26 2016 -0600 @@ -0,0 +1,84 @@ +// not used, just for reference + +package luan.modules.url; + +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.Reader; +import java.io.IOException; +import java.net.URLConnection; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.Map; +import java.util.HashMap; +import luan.modules.Utils; + + +public final class UrlCall { + public final URLConnection connection; + + public UrlCall(String url) throws IOException { + this(new URL(url)); + } + + public UrlCall(URL url) throws IOException { + connection = url.openConnection(); + } + + public void acceptJson() { + connection.setRequestProperty("accept","application/json"); + } + + public String get() throws IOException { + Reader in = new InputStreamReader(connection.getInputStream()); + String rtn = Utils.readAll(in); + in.close(); + return rtn; + } + + public String post(String content,String contentType) throws IOException { + HttpURLConnection connection = (HttpURLConnection)this.connection; + + connection.setRequestProperty("Content-type",contentType); + connection.setDoOutput(true); + connection.setRequestMethod("POST"); + + byte[] post = content.getBytes(); + connection.setRequestProperty("Content-Length",Integer.toString(post.length)); + OutputStream out = connection.getOutputStream(); + out.write(post); + out.flush(); + + Reader in; + try { + in = new InputStreamReader(connection.getInputStream()); + } catch(IOException e) { + InputStream is = connection.getErrorStream(); + if( is == null ) + throw e; + in = new InputStreamReader(is); + String msg = Utils.readAll(in); + in.close(); + throw new UrlCallException(msg,e); + } + String rtn = Utils.readAll(in); + in.close(); + out.close(); + return rtn; + } + + public String post(String content) throws IOException { + return post(content,"application/x-www-form-urlencoded"); + } + + public String postJson(String content) throws IOException { + return post(content,"application/json"); + } + + public static final class UrlCallException extends IOException { + UrlCallException(String msg,IOException e) { + super(msg,e); + } + } +}