客户端REST的GET请求(客户端)

客户端写REST的GET请求,只需要使用HttpURLConnection访问资源路径即可

代码如下:

//创建url
URL url = new URL("http://127.0.0.1:12345/user/student/query/23?_type=json");

//打开连接
HttpURLConnection con = (HttpURLConnection) url.openConnection();

//设置请求方式
con.setRequestMethod("GET");
/**默认connection没有读写的权限*/
con.setDoOutput(true);

//获取响应
int code = con.getResponseCode();
if(code == 200){
	System.out.println("响应成功");
	//读取数据 
	InputStream is = con.getInputStream();
	InputStreamReader isr = new InputStreamReader(is);
	BufferedReader br = new BufferedReader(isr);
	String line = null;
	while((line = br.readLine()) != null){
		System.out.println(line);
	}
	br.close();
	isr.close();
	is.close();
}else{
	System.out.println("响应失败");
}

你可能感兴趣的:(Webservice总结)