HttpURLConnection 实现调用其他服务接口

HttpURLConnection 实现调用其他服务接口


工具类

代码如下(示例):

 public static String doRequest(String location , String requestMethod ,String token, String params,String encoding) throws Exception{
        URL url = new URL(location);
        HttpURLConnection connection =(HttpURLConnection) url.openConnection();
        connection.setRequestMethod(requestMethod); // 设置请求方式 GET、POST 等
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("Authorization","Bearer ["+token+"]");
        connection.setConnectTimeout(2000); // 设置连接超时时间,单位:ms 。
        connection.setReadTimeout(2000); // 设置读取超时时间,单位:ms 。
        connection.setDoInput(true); // 设置打开输入流 : default = true
        connection.setDoOutput(true); // 设置打开输出流:default = false
        connection.setUseCaches(false); // 设置是否启用用户缓存: default = false
        OutputStream outputStream = connection.getOutputStream(); // 获取输出流对象,准备往服务器写数据
        outputStream.write(params.getBytes());
        outputStream.flush();
        outputStream.close();

        // 获取服务器返回的响应状态
        if(connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
            System.out.println(" request fail ...");
            return connection.getResponseMessage();
        }

        // 读取服务器返回的数据
        BufferedReader reader = new BufferedReader(new
                InputStreamReader(connection.getInputStream(),encoding));
        String line ;
        StringBuffer sb = new StringBuffer();
        while((line = reader.readLine()) != null) {
            sb.append(line);
        }
        reader.close();
        String result = sb.toString();
        return result ;
    }

总结

项目中使用记录。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,可以的,以下是使用HttpURLConnection调用第三方WebService接口的基本步骤: 1. 创建URL对象,设置目标WebService地址。 2. 使用URL对象创建HttpURLConnection对象。 3. 设置HttpURLConnection对象的请求方法为POST。 4. 设置HttpURLConnection对象的连接超时时间和读取超时时间。 5. 设置HttpURLConnection对象的请求头信息。 6. 设置HttpURLConnection对象的请求体信息。 7. 发送请求,获取响应。 8. 解析响应,获取需要的数据。 具体实现细节可以参考以下示例代码,供您参考: ``` URL url = new URL("http://xxxxxx:8080/xxxxx/xxxxx");//WebService地址 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST");//请求方式为POST conn.setConnectTimeout(5000);//连接超时时间为5秒 conn.setReadTimeout(5000);//读取超时时间为5秒 conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");//设置请求头信息 String soapXml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://service.xxx.com/\">" + "<soapenv:Header/>" + "<soapenv:Body>" + "<ser:xxxxx>" + "<ser:xxxxx>xxxxx</ser:xxxxx>" + "<ser:xxxxx>xxxxx</ser:xxxxx>" + "</ser:xxxxx>" + "</soapenv:Body>" + "</soapenv:Envelope>"; conn.setRequestProperty("Content-Length", String.valueOf(soapXml.getBytes().length));//设置请求体长度 conn.setDoOutput(true);//设置允许输出 OutputStream os = conn.getOutputStream(); os.write(soapXml.getBytes());//写入请求体信息 os.flush(); os.close(); if (conn.getResponseCode() == 200) {//获取响应码 InputStream is = conn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line); } is.close(); reader.close(); String result = sb.toString();//获取响应数据 //解析响应数据,获取需要的数据 } else { //请求失败处理逻辑 } conn.disconnect();//关闭连接 ``` 需要注意的是,以上示例代码中的SOAP请求体格式是一种常见的WebService请求格式,如果需要调用其他格式的WebService接口,请求体格式可能会有所不同。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值