您的位置:首页 > 理论基础 > 计算机网络

使用HttpClient调用RESTful Web服务

2016-05-30 09:27 821 查看
调用代码:

public class HttpClientUtil {

public static void callSVR(final String URL) {
try {
HttpClient httpClient = new DefaultHttpClient();
// 这里默认GET方法,HttpClient同时支持POST、PUT等方法
HttpGet req = new HttpGet(URL);
req.addHeader("Accept", "text/html");
HttpResponse resp = httpClient.execute(req);
HttpEntity entity = resp.getEntity();
InputStream input = entity.getContent();
String result = read(input);
System.out.println(result);
} catch (Exception e) {
}
}

private static String read(InputStream input) {
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(input));
String line = null;
try {
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
}
return sb.toString();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  web服务 HttpClient java