笔记-RestTemplate,WebService

笔记-RestTemplate,WebService

RestTemplate

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.codec.binary.Base64;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

public class RestTemplateTest {
    public static void main(String[] args) {
        // 定义请求接口URL
        String baseUrl = "http://localhost:8088/";

        // 定义header对象
        HttpHeaders headers = new HttpHeaders();
        //设置请求头
        headers.set("contentType", "application/json;charset=UTF-8");
        // 接口启用了Basic认证,那么客户端需要加入认证header信息
        //String username = ""; // 认证用户名
        //String password = ""; // 认证密码
        //addAuth(headers, username + ":" + password);

        sendGet(baseUrl,headers);
       // sendPost(baseUrl,headers);
    }

    /**
     * post请求测试
     * @param baseUrl
     * @param headers
     */
    public static void sendPost(String baseUrl, HttpHeaders headers) {
        String requestUrl = baseUrl + "api/user/list";
        // 定义请求参数Map
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("pageNum","1");
        paramsMap.put("pageSize","20");
        paramsMap.put("role","用户");
        // 定义http请求实体对象
        HttpEntity<HashMap<String, String>> entity = new HttpEntity<>(paramsMap, headers);
        // 发送请求
        RestTemplate template = new RestTemplate();
        //Map返回类型 HttpMethod.POST 请求类型
        ResponseEntity<Map> exchange = template.exchange(requestUrl, HttpMethod.POST, entity, Map.class);
        System.out.println("exchange.getBody()" + exchange.getBody());
    }


    /**
     * get请求测试
     * @param baseUrl
     * @param headers
     */
    public static void sendGet(String baseUrl, HttpHeaders headers) {
        String requestUrl = baseUrl +"api/login/test?phone={phone}&password={password}";
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("phone","1361111111");
        paramsMap.put("password","123456");
        // 发送请求
        RestTemplate template = new RestTemplate();
        //Map返回类型 HttpMethod.POST 请求类型
        ResponseEntity<String> exchange = template.getForEntity(requestUrl,String.class,paramsMap);
        System.out.println("exchange.getBody()" + exchange.getBody());
    }


    /**
     * 编码
     * @param headers
     * @param yourEncryptedWorlds
     */
    private static void addAuth(HttpHeaders headers, String yourEncryptedWorlds) {
        byte[] key = (yourEncryptedWorlds).getBytes();
        String authHeader = new String(Base64.encodeBase64(key));
        headers.set("Authorization", "Basic "+authHeader);
    }
}

WebService

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class WebServiceTest {
    public static void main(String[] args) throws IOException {
        // 第一步:创建服务地址
        URL url = new URL("http://localhost:8080/webservice/user/getUersList?wsdl");
        // 第二步:打开一个通向服务地址的连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        // 第三步:设置参数
        // 3.1发送方式设置:POST必须大写
        connection.setRequestMethod("POST");
        // 3.2设置数据格式:content-type
        connection.setRequestProperty("content-type", "text/xml;charset=utf-8");
        // 3.3设置输入输出,因为默认新创建的connection没有读写权限,
        connection.setDoInput(true);
        connection.setDoOutput(true);

        // 第四步:组织SOAP数据,发送请求
        String username = "";
        String password = ""; //密码md5加密
        String param = "";
        String soapXML = getXML(param, username, password);
        // 将信息以流的方式发送出去
        OutputStream os = connection.getOutputStream();
        os.write(soapXML.getBytes());
        // 第五步:接收服务端响应,打印
        int responseCode = connection.getResponseCode();
        if (200 == responseCode) {// 表示服务端响应成功
            // 获取当前连接请求返回的数据流
            InputStream is = connection.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);

            StringBuilder sb = new StringBuilder();
            String temp = null;
            while (null != (temp = br.readLine())) {
                sb.append(temp);
            }

            /**
             * 打印结果
             */
            System.out.println(sb.toString());

            is.close();
            isr.close();
            br.close();
        }
        os.close();
    }


    /**
     * 拼接参数
     * @param param
     * @param username
     * @param password
     * @return
     */
    public static String getXML(String param, String username, String password) {
        //使用SoapUI 获取soapXML
        String soapXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                + "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://webservice.carmng.gacc.kmss.landray.com/\">"
                + "<soapenv:Header>" + "<tns:RequestSOAPHeader xmlns:tns=\"http://sys.webservice.client\">"
                + "<tns:user>" + username + "</tns:user>" + "<tns:password>" + password + "</tns:password>"
                + "</tns:RequestSOAPHeader>" + "</soapenv:Header>" + " <soapenv:Body>" + " <web:getUserList>" +param+ " </web:getUserList>" + "</soapenv:Body>" + "</soapenv:Envelope>";
        return soapXML;
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值