使用HttpClient调用RESTful Web服务

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ZhuangM_888/article/details/51535549

调用代码:

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();
    }
}

猜你喜欢

转载自blog.csdn.net/ZhuangM_888/article/details/51535549