一起来学REST(12.2)——Java中使用REST
2011-08-27 23:52
211 查看
原文地址:http://rest.elkstein.org/
主要的类是HttpURLConnection,通过对一个URL调用openConnection可以得到这个类,openConnection方法的签名指向一个超类URLConnection,我们还需要对其进行类类型向下转型。
下面的方法发送一个请求,并且返回一个长字符串:
(这段代码有些粗糙,需要加上适当的try/catch/finally来保证reader可以关闭,等等。
记住,如果URL包含参数,必须进行适当的编码(例如空格是%20,等等)。类URLEncoder用来进行这样的编码。
发送HTTP POST请求
在POST请求中的URL也需要编码,如下面的方法所示:
As you can see, it's not a pretty site (and that's before adding proper try/catch/finally structures). The problem is that, out of the box, Java's support for handling web connections is pretty low-level.
A good solution can be found in the popular
Apache Commons library, and in particular the
httpclient set of packages. See
Yahoo! guide to REST with Java for details and examples. The documentation covers several interesting extras, such ascaching.
By Dr. M. Elkstein
Learn REST: A Tutorial
发送HTTP GET请求主要的类是HttpURLConnection,通过对一个URL调用openConnection可以得到这个类,openConnection方法的签名指向一个超类URLConnection,我们还需要对其进行类类型向下转型。
下面的方法发送一个请求,并且返回一个长字符串:
public static String httpGet(String urlStr) throws IOException { URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); if (conn.getResponseCode() != 200) { throw new IOException(conn.getResponseMessage()); } // Buffer the result into a string BufferedReader rd = new BufferedReader( new InputStreamReader(conn.getInputStream())); StringBuilder sb = new StringBuilder(); String line; while ((line = rd.readLine()) != null) { sb.append(line); } rd.close(); conn.disconnect(); return sb.toString(); }
(这段代码有些粗糙,需要加上适当的try/catch/finally来保证reader可以关闭,等等。
记住,如果URL包含参数,必须进行适当的编码(例如空格是%20,等等)。类URLEncoder用来进行这样的编码。
发送HTTP POST请求
在POST请求中的URL也需要编码,如下面的方法所示:
public static String httpPost(String urlStr, String[] paramName, String[] paramVal) throws Exception { URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setDoInput(true); conn.setUseCaches(false); conn.setAllowUserInteraction(false); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // Create the form content OutputStream out = conn.getOutputStream(); Writer writer = new OutputStreamWriter(out, "UTF-8"); for (int i = 0; i < paramName.length; i++) { writer.write(paramName[i]); writer.write("="); writer.write(URLEncoder.encode(paramVal[i], "UTF-8")); writer.write("&"); } writer.close(); out.close(); if (conn.getResponseCode() != 200) { throw new IOException(conn.getResponseMessage()); } // Buffer the result into a string BufferedReader rd = new BufferedReader( new InputStreamReader(conn.getInputStream())); StringBuilder sb = new StringBuilder(); String line; while ((line = rd.readLine()) != null) { sb.append(line); } rd.close(); conn.disconnect(); return sb.toString(); }
As you can see, it's not a pretty site (and that's before adding proper try/catch/finally structures). The problem is that, out of the box, Java's support for handling web connections is pretty low-level.
A good solution can be found in the popular
Apache Commons library, and in particular the
httpclient set of packages. See
Yahoo! guide to REST with Java for details and examples. The documentation covers several interesting extras, such ascaching.
By Dr. M. Elkstein
相关文章推荐
- 使用iServer REST API发布工作空间(Java)
- 最给力的java REST入门:使用 JAX-RS 简化 REST 应用开发(转自IBM)
- java REST入门:使用 JAX-RS 简化 REST 应用开发
- Linux上使用java代码实现Local Interactive Commands(such as:hprest;hpssacli)
- java后端发送http请求使用RestTemplate(简单的都无敌了)
- rest注解说明及使用示例(java)
- Java 不使用第三方类库实现REST调用
- 爽翻天!告别Java。一起来使用kotlin开发完整客户端
- 【异常】关于使用ES的RestHighLevel JavaAPI执行search操作时,参数的问题
- java使用rest api登录salesforce | How to do authentication to salesforce from java class using Rest API
- REST 在 Java 中的使用
- 使用Java Rest Client操作Elasticsearch
- 爽翻天!告别Java。一起来使用kotlin开发完整客户端
- REST 在 Java 中的使用
- java开发中使用CXF发布rest风格的webservice服务
- 最给力的java REST入门:使用 JAX-RS 简化 REST 应用开发(转自IBM)
- 使用Java Rest Client操作Elasticsearch
- 一起来学REST(12.1)——C#中使用REST
- 使用jersey(2.5.1)编写Java REST客户端
- 使用jersey(2.5.1)搭建java REST服务