您的位置:首页 > 理论基础 > 计算机网络

HttpURLConnection 直接发送soap消息调用webservice

2017-02-20 09:38 323 查看
import Java.io.BufferedReader;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.NET.HttpURLConnection;

import java.Net.URL;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Date;

public class aaa {

 

 private String uploadPcrf(String xml){

      String urlString="http://135.64.209.137:8080/axis/services:ScfPccSoapServiceEndpointPort";

      HttpURLConnection httpConn = null;

   OutputStream out = null;

   String returnXml="";

   

    

   //设置批次号

       DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");

      String s= df.format(new Date());

      System.out.println("开始");

   try {

    httpConn = (HttpURLConnection) new URL(urlString).openConnection();

    httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");

//    httpConn.setRequestProperty("SOAPAction", soapActionString);

    httpConn.setRequestMethod("POST");

    httpConn.setDoOutput(true);

    httpConn.setDoInput(true);

    httpConn.connect();

    out = httpConn.getOutputStream(); // 获取输出流对象

    httpConn.getOutputStream().write(xml.getBytes("utf-8")); // 将要提交服务器的SOAP请求字符流写入输出流,注意使用utf-8编码
    out.flush();
    out.close();
    int code = httpConn.getResponseCode(); // 用来获取服务器响应状态
    String tempString = null;
    StringBuffer sb1 = new StringBuffer();
    if (code == HttpURLConnection.HTTP_OK) {
     BufferedReader reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), "UTF-8"));
     while ((tempString = reader.readLine()) != null) {
      sb1.append(tempString);
     }
     if (null != reader) {
      reader.close();
     }
    } else {
     BufferedReader reader = new BufferedReader(new InputStreamReader(httpConn.getErrorStream(), "UTF-8"));
     // 一次读入一行,直到读入null为文件结束
     while ((tempString = reader.readLine()) != null) {
      sb1.append(tempString);
     }
     if (null != reader) {
      reader.close();
     }
    } 
    //响应报文
    returnXml=sb1.toString();
    System.out.println("结束");
   } catch (Exception e) {    
   } 
      return returnXml;
     }
 
 private static String getXmlHead(){
  String retunStr="<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
  retunStr +="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"
xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:rm=\"rm:soap\">";
  retunStr +="<soapenv:Header/>";
  //retunStr +="<spr:Address>"+completionBO.getPcrfUrl()+"</spr:Address>"; 
  //SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
  //retunStr +="<spr:MessageID>"+df.format(new Date())+"</spr:MessageID>"; 
  //retunStr +="<spr:Password>"+completionBO.getPcrfPassword() +"</spr:Password>";
  //retunStr +="<spr:Username>"+completionBO.getPcrfUserName()+"</spr:Username>";
  
  //retunStr +="</SOAP-ENV:Header>";
  retunStr +="<soapenv:Body>";
  return retunStr;
 }
    private static String getXmlTail(){
     String retunStr="</soapenv:Body>";
     retunStr+="</soapenv:Envelope>";
     return retunStr;
    }
  
 public static void main(String[] args) {
  
  String xml ="";
  xml = getXmlHead();
  xml += "<rm:getSubscriberAllService><inPara><subscriber><attribute><key>usrIdentifier</key><value>8618900002781</value></attribute></subscriber></inPara></rm:getSubscriberAllService>";
  xml += getXmlTail();
  String r = new aaa().uploadPcrf(xml);
  System.out.println(r);
 }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: