HttpURLConnection 直接发送soap消息调用webservice
2015-09-02 14:07
615 查看
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()); // 将要提交服务器的SOAP请求字符流写入输出流
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);
}
}
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()); // 将要提交服务器的SOAP请求字符流写入输出流
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);
}
}
相关文章推荐
- HTTP Client使用
- 利用Ihttpmodel实现网站缓存,解决Server.Transfer 直接输出HTML源代码的问题
- SDP协议介绍
- (转)epoll源码分析 http://www.cnblogs.com/debian/archive/2012/02/16/2354454.html
- ASIHTTPRequest使用指南---<<翻译稿>>老外的很有价值
- http协议
- http://downloads.openwrt.org.cn/sources/
- 无源光网络PON的原理
- Node.js 0.12: 正确发送HTTP POST请求
- 如何使用ASIHTTPRequest来进行网络请求的
- HTTP请求报文和HTTP响应报文
- HTTP中的POST和GET请求的本质区别详解
- linux下网络环境配置
- HTTP必死:Google是怎么考虑安全的?
- Android Https相关完全解析 当OkHttp遇到Https
- JAVA发送HTTP请求,并接受返回内容
- 发送http请求
- 一张图读懂https加密协议
- 如何申请https证书、搭建https网站
- 使用TCP协议的NAT穿透技术(转)