zoukankan      html  css  js  c++  java
  • 调用RESTful GET方法

    package restclient;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    public class RestClient {
    
        private static final String targetURL = "http://localhost:8080/simple-service-webapp/webapi/myresource";
    
        public static void main(String[] args) {
            // TODO code application logic here
            try {
                URL restServiceURL = new URL(targetURL);
    
                HttpURLConnection httpConnection = (HttpURLConnection) restServiceURL.openConnection();
                httpConnection.setRequestMethod("GET");
                httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    
                if (httpConnection.getResponseCode() != 200) {
                    throw new RuntimeException("HTTP GET Request Failed with Error code : " + httpConnection.getResponseCode());
                }
    
                BufferedReader responseBuffer = new BufferedReader(new InputStreamReader((httpConnection.getInputStream())));
    
                String output;
                System.out.println("Output from Server:  \n");
    
                while ((output = responseBuffer.readLine()) != null) {
                    System.out.println(output);
                }
    
                httpConnection.disconnect();
    
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }        
        }
    }
    
  • 相关阅读:
    应用提交到安卓应用市场需要注意哪些地方?
    chromedriver 下载
    缺陷与测试报告
    需求分析与测试计划、方案
    【转】使用信号监控 Django 模型对象字段值的变化
    缓存技术
    Tomcat 代码方式启动
    枚举类型 (币种例子)
    SpringMVC 常用注解
    HttpClient 教程
  • 原文地址:https://www.cnblogs.com/ccskun/p/5577378.html
Copyright © 2011-2022 走看看