我试图了解如何向服务器发送REST请求。如果我必须使用httpconnections或任何其他连接在java中将其实现为请求,我该怎么做?
POST /resource/1
Host: myownHost
DATE: date
Content-Type: some standard type
这应该如何以标准方式构建?
URL url= new URL("http://myownHost/resource/1");
HttpsURLConnection connect= (HttpsURLConnection) url.openConnection();
connect.setRequestMethod("POST");
connect.setRequestProperty("Host", "myOwnHost");
connect.setRequestProperty("Date","03:14:15 03:14:15 GMT");
connect.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
答案 0 :(得分:2)
有很多选项,Apache HTTP客户端(http://hc.apache.org/httpcomponents-client-4.4.x/index.html)就是其中之一(并且让事情变得非常简单)
创建REST请求可以像这样简单(在这种情况下使用JSON):
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet getRequest = new HttpGet(
"http://localhost:8080/RESTfulExample/json/product/get");
getRequest.addHeader("accept", "application/json");
HttpResponse response = httpClient.execute(getRequest);
if (response.getStatusLine().getStatusCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatusLine().getStatusCode());
}
BufferedReader br = new BufferedReader(
new InputStreamReader((response.getEntity().getContent())));
更新:抱歉,文档的链接已更新。发布了新文档。
答案 1 :(得分:0)
你应该在这里使用json
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class NetClientPost {
// http://localhost:8080/RESTfulExample/json/product/post
public static void main(String[] args) {
try {
URL url = new URL("http://myownHost/resource/1");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
String input = "{\"DATE\":\"03:14:15 03:14:15 GMT\",\"host\":\"myownhost\"}";
OutputStream os = conn.getOutputStream();
os.write(input.getBytes());
os.flush();
if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
更多内容浏览link
答案 2 :(得分:0)
有几种方法可以使用Java调用RESTful服务,但不需要使用原始级API; - )
它存在一些RESTful框架,如Restlet或JAX-RS。它们针对客户端和服务器端,旨在隐藏此类调用的技术管道。下面是一个代码示例,描述了如何使用Restlet和JSON解析器进行处理:
JSONObject jsonObj = new JSONObject();
jsonObj.put("host", "...");
ClientResource cr = new Client("http://myownHost/resource/1");
cr.post(new JsonRepresentation(jsonObject);
// In the case of form
// Form form = new Form ();
// form.set("host", "...");
// cr.post(form);
您可以注意到,在上一个代码段中,根据您发送的内容(表单,JSON,...)自动为您设置标题Content-type
,Date
除非是一个小注释,要添加元素,您应该在元素列表资源(POST
)或方法http://myownHost/resources/
上使用方法PUT
,如果您有所需的唯一标识符用来识别它(http://myownHost/resources/1
)。此链接可能对您有用:https://templth.wordpress.com/2014/12/15/designing-a-web-api/。
希望它可以帮到你, 亨利