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; }
  • 相关阅读:
    【转】 UI自动化测试的关注点
    使用MapReduce将HDFS数据导入到HBase(一)
    Hadoop2.4.1 MapReduce通过Map端shuffle(Combiner)完成数据去重
    Hadoop2.4.1 使用MapReduce简单的数据清洗
    Hadoop2.4.1 64-Bit QJM HA and YARN HA + Zookeeper-3.4.6 + Hbase-0.98.8-hadoop2-bin HA Install
    hadoop2.2.0 MapReduce求和并排序
    hadoop2.2.0 MapReduce分区
    hadoop2.2.0伪分布模式64位安装
    hadoop2.2.0 MapReduce的序列化
    MyEclipse8.6下的svn插件安装
  • 原文地址:https://www.cnblogs.com/benbencyb/p/14468786.html
Copyright © 2011-2022 走看看