zoukankan      html  css  js  c++  java
  • Java调用webservice请求

    基于SOAP协议的WEB服务调用方式:

    
    
    import org.apache.commons.lang.StringEscapeUtils;
    import org.apache.log4j.Logger;

    /**
    * webservice请求 * @param xmlStr * @return * @throws Exception */ public static String callXml(String xmlStr, String soapAddress) throws IOException { //地址 URL url = new URL(soapAddress); //调用的方法 String soapActionString = ""; logger.info("请求SOAP地址:"+soapAddress); logger.info("请求SOAPAction:"+soapActionString); //打开链接 HttpURLConnection con = (HttpURLConnection) url.openConnection(); logger.info("请求报文:"+xmlStr); //设置好header信息 con.setRequestMethod("POST"); con.setRequestProperty("Content-Type", "text/xml;charset=UTF-8"); con.setRequestProperty("Content-Length", String.valueOf(xmlStr.getBytes().length)); con.setRequestProperty("SOAPAction", soapActionString); //post请求需要设置 con.setDoOutput(true); con.setDoInput(true); //对请求body 往里写xml 设置请求参数. PrintWriter out = null; byte[] responseData = null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { out = new PrintWriter(new OutputStreamWriter(con.getOutputStream(),"utf-8")); // 发送请求参数 out.print(xmlStr); out.flush(); //设置响应回来的信息 InputStream ips = con.getInputStream(); byte[] buf = new byte[1024]; int length = 0; while( (length = ips.read(buf)) != -1){ baos.write(buf, 0, length); baos.flush(); } responseData = baos.toByteArray(); } catch (IOException e) { throw new IOException(e); } finally { if (out != null) { out.close(); } try { baos.close(); } catch (IOException e) { throw new IOException(e); } con.disconnect(); } //处理写响应信息 String responseMess = new String(responseData,"utf-8"); responseMess = StringEscapeUtils.unescapeHtml(responseMess); logger.info("响应码:"+con.getResponseCode()); logger.info("响应报文:"+responseMess); return responseMess; }
  • 相关阅读:
    Go语言基础--1.1 变量的声明
    基本语法
    弹性盒子修改
    弹性盒子内容
    弹性盒子
    响应式列重置
    栅格系统
    布局容器
    额外按钮
    可消失的弹出框
  • 原文地址:https://www.cnblogs.com/benbencyb/p/14468786.html
Copyright © 2011-2022 走看看