java调用webService客户端的实现

小马子 2012-08-17 03:34:52
哪们大侠给指点一下:我这里用apach 的axis2调用别人给的一个webService接口,调用方法如下
package com.cstc.service;

import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;

import com.cstc.util.DESCoder;


public class QueryService {
public void queryPersonInfoByName(String idno,String cid){
System.out.println("idno+cid"+idno+" "+cid);
try{
String sKey = "CSgjjWeb";
String endpoint = "http://www.csgjj.com.cn:9001/QueryService.asmx?WSDL";
Service service = new Service();
Call call = (Call)service.createCall();
call.setTargetEndpointAddress(new URL(endpoint));
call.setOperationName(new QName("http://www.csgjj.com.cn:9001/WebService", "QueryPersonalInfo"));
call.addParameter(new QName("http://www.csgjj.com.cn:9001/WebService", "strPersonalName"), XMLType.XSD_STRING, ParameterMode.IN);
call.addParameter(new QName("http://www.csgjj.com.cn:9001/WebService","strIdno"), XMLType.XSD_STRING, ParameterMode.IN);
call.addParameter(new QName("http://www.csgjj.com.cn:9001/WebService","strCustomerID"), XMLType.XSD_STRING, ParameterMode.IN);
call.setUseSOAPAction(true);
call.setSOAPActionURI("http://www.csgjj.com.cn:9001/WebService/QueryPersonalInfo");
call.setReturnType(XMLType.XSD_STRING);
String strPersonalName = DESCoder.EncryptAsDoNet(new String("唐晓晖".getBytes("UTF-8")),sKey);
String strIdno = DESCoder.EncryptAsDoNet(idno,sKey);
String strCustomerID = DESCoder.EncryptAsDoNet(cid,sKey);
Object[] params = new Object[3];
params[0] = strPersonalName;
params[1] = strIdno;
params[2] = strCustomerID;
String res = (String)call.invoke(params);
String resDe = DESCoder.DecryptDoNet(res,sKey);
// System.out.println("Right: " + "IbN+8kIFNo/w2Q9GnBI82w==");
// System.out.println("Current: " + (String)params[0]);
System.out.println(resDe);
System.out.println("over queryService");

}catch (Exception ex){
System.out.println("Error: " + ex.getMessage());
}
System.out.println("over catch hou");
}
public static void main(String args[]){
QueryService qs =new QueryService();
qs.queryPersonInfoByName("430104197105050796","12580");
}
}
在这里用的main方法进行的调用,是可以查到想要的数据的。但是如果在servlet中进行调用返回的数据是空的,也没有任何错误提示,代码如下:
public class InsuranceServlet extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


QueryService qs =new QueryService();
qs.queryPersonInfoByName("430104197105050796","12580");
System.out.println("over InsuranceServlet");
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}

}
这样查到的结果是空,这是为什么呀?没有错误提示,程序已经执行完毕,高手请指点,谢啦!
...全文
961 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
何之遇 2013-08-20
  • 打赏
  • 举报
回复
引用 12 楼 mhl316910594 的回复:
我跟过的,问题应该是出在加密这里,就是说从main里边和servlet里边传过来的中文名字是相同的测字,但加密出来的结果是不一样的,这一点我非常不能理解,麻烦帮忙看下
我也遇到同样的问题,在servlet中加密的结果和main中不一样
chenw323 2012-08-21
  • 打赏
  • 举报
回复
使用axis2调用wenbservice 可以直接用wsdl2java把wsdl转成本地代码
比自己写代码方便多了啊
小马子 2012-08-21
  • 打赏
  • 举报
回复
我跟过的,问题应该是出在加密这里,就是说从main里边和servlet里边传过来的中文名字是相同的测字,但加密出来的结果是不一样的,这一点我非常不能理解,麻烦帮忙看下
cscript 2012-08-21
  • 打赏
  • 举报
回复
既然main函数没有错误,建议楼主在servlet运行debug跟踪下,找出问题
小马子 2012-08-21
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 的回复:]
DESCoder 能否把这个类贴上来?
[/Quote]
DESCoder 是一个加密和解密的类代码如下:
public abstract class DESCoder {
/*
* 加密字符串
*/
public static String EncryptAsDoNet(String message, String key) throws Exception {
//产生与.net 对应的加密des + base64 加密串
//message = java.net.URLEncoder.encode(message, "ascii");

Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("GB2312"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
IvParameterSpec iv = new IvParameterSpec(key.getBytes("GB2312"));
cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
byte[] encryptbyte = cipher.doFinal(message.getBytes());
BASE64Encoder base64Encoder = new BASE64Encoder();
//base64Encoder.encode(encryptbyte);
return base64Encoder.encode(encryptbyte);
//toHexString(encryptbyte).toUpperCase();

}
}
麻烦帮忙看下,谢啦!
fanket2002 2012-08-21
  • 打赏
  • 举报
回复
DESCoder 能否把这个类贴上来?
fanket2002 2012-08-21
  • 打赏
  • 举报
回复
为什么你的servlet里面没有初始化方法和销毁方法?
s478853630 2012-08-21
  • 打赏
  • 举报
回复
main方法可以运行就意味着只要有java代码的任何角落都可以运行,
记得大一初学java时,老师是这么说的!

不知道你那是个啥情况,最好能上门服务!!呵呵!
小马子 2012-08-21
  • 打赏
  • 举报
回复
并没有解决我的问题呀?我的那种方式用main方法测试也是可行的,有正确的数据返回,可是为什么用Servlet调用就返回不了结果呢?哪位大师帮帮忙给看下啦,很急的!(补充:我用java远程donet的webService接口)
cscript 2012-08-21
  • 打赏
  • 举报
回复
不需要通用的话,直接用HttpConnection或HttpClient模拟提交行了,用那些axis或cxf 太麻烦了
s478853630 2012-08-21
  • 打赏
  • 举报
回复
呵呵!
这下代码的原作并不是鄙人,鄙人也是从一个前辈那里学来的。
像这些代码最好去学习前辈的,要等自己写出来,够呛!!!

建议lz最好是去下载demo,因为运行这段代码需要20个jar的支持,少一个都不行!
for_my_chen 2012-08-21
  • 打赏
  • 举报
回复
这么多代码。。。。。苦逼的程序员啊,,,,耗费精力啊
howtodown 2012-08-21
  • 打赏
  • 举报
回复
不容易啊
s478853630 2012-08-21
  • 打赏
  • 举报
回复
在浏览器输入这个地址:http://localhost:8088/bank/services/bankService?wsdl
得到以下结果,说明这个webservice是可用的!

Java code
<?xml version="1.0" encoding="UTF-8" ?>
- <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:ns="http://service.bank.com" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:ax21="http://entity.bank.com/xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" targetNamespace="http://service.bank.com">
<wsdl:documentation>bankService</wsdl:documentation>
- <wsdl:types>
- <xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://entity.bank.com/xsd">
- <xs:complexType name="Bank">
- <xs:sequence>
<xs:element minOccurs="0" name="account" nillable="true" type="xs:string" />
<xs:element minOccurs="0" name="balance" nillable="true" type="xs:float" />
<xs:element minOccurs="0" name="bankName" nillable="true" type="xs:string" />
<xs:element minOccurs="0" name="money" nillable="true" type="xs:float" />
<xs:element minOccurs="0" name="opraStatus" nillable="true" type="xs:string" />
<xs:element minOccurs="0" name="password" nillable="true" type="xs:string" />
<xs:element minOccurs="0" name="prompt" nillable="true" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>
- <xs:schema xmlns:ax22="http://entity.bank.com/xsd" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://service.bank.com">
<xs:import namespace="http://entity.bank.com/xsd" />
- <xs:element name="transition">
- <xs:complexType>
- <xs:sequence>
<xs:element minOccurs="0" name="fromBack" nillable="true" type="ax22:Bank" />
<xs:element minOccurs="0" name="toBank" nillable="true" type="ax22:Bank" />
</xs:sequence>
</xs:complexType>
</xs:element>
- <xs:element name="transitionResponse">
- <xs:complexType>
- <xs:sequence>
<xs:element minOccurs="0" name="return" nillable="true" type="ax22:Bank" />
</xs:sequence>
</xs:complexType>
</xs:element>
- <xs:element name="main">
- <xs:complexType>
- <xs:sequence>
<xs:element maxOccurs="unbounded" minOccurs="0" name="args" nillable="true" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
</wsdl:types>
- <wsdl:message name="mainRequest">
<wsdl:part name="parameters" element="ns:main" />
</wsdl:message>
- <wsdl:message name="transitionRequest">
<wsdl:part name="parameters" element="ns:transition" />
</wsdl:message>
- <wsdl:message name="transitionResponse">
<wsdl:part name="parameters" element="ns:transitionResponse" />
</wsdl:message>
- <wsdl:portType name="bankServicePortType">
- <wsdl:operation name="main">
<wsdl:input message="ns:mainRequest" wsaw:Action="urn:main" />
</wsdl:operation>
- <wsdl:operation name="transition">
<wsdl:input message="ns:transitionRequest" wsaw:Action="urn:transition" />
<wsdl:output message="ns:transitionResponse" wsaw:Action="urn:transitionResponse" />
</wsdl:operation>
</wsdl:portType>
- <wsdl:binding name="bankServiceSoap11Binding" type="ns:bankServicePortType">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
- <wsdl:operation name="main">
<soap:operation soapAction="urn:main" style="document" />
- <wsdl:input>
<soap:body use="literal" />
</wsdl:input>
</wsdl:operation>
- <wsdl:operation name="transition">
<soap:operation soapAction="urn:transition" style="document" />
- <wsdl:input>
<soap:body use="literal" />
</wsdl:input>
- <wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
- <wsdl:binding name="bankServiceSoap12Binding" type="ns:bankServicePortType">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
- <wsdl:operation name="main">
<soap12:operation soapAction="urn:main" style="document" />
- <wsdl:input>
<soap12:body use="literal" />
</wsdl:input>
</wsdl:operation>
- <wsdl:operation name="transition">
<soap12:operation soapAction="urn:transition" style="document" />
- <wsdl:input>
<soap12:body use="literal" />
</wsdl:input>
- <wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
- <wsdl:binding name="bankServiceHttpBinding" type="ns:bankServicePortType">
<http:binding verb="POST" />
- <wsdl:operation name="main">
<http:operation location="bankService/main" />
- <wsdl:input>
<mime:content type="text/xml" part="main" />
</wsdl:input>
</wsdl:operation>
- <wsdl:operation name="transition">
<http:operation location="bankService/transition" />
- <wsdl:input>
<mime:content type="text/xml" part="transition" />
</wsdl:input>
- <wsdl:output>
<mime:content type="text/xml" part="transition" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
- <wsdl:service name="bankService">
- <wsdl:port name="bankServiceHttpSoap11Endpoint" binding="ns:bankServiceSoap11Binding">
<soap:address location="http://192.168.1.96:8088/bank/services/bankService.bankServiceHttpSoap11Endpoint/" />
</wsdl:port>
- <wsdl:port name="bankServiceHttpSoap12Endpoint" binding="ns:bankServiceSoap12Binding">
<soap12:address location="http://192.168.1.96:8088/bank/services/bankService.bankServiceHttpSoap12Endpoint/" />
</wsdl:port>
- <wsdl:port name="bankServiceHttpEndpoint" binding="ns:bankServiceHttpBinding">
<http:address location="http://192.168.1.96:8088/bank/services/bankService.bankServiceHttpEndpoint/" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>



然后在另外一个项目中测试调用:

Java codepackage com.axis2.test;

import javax.xml.namespace.QName;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import com.axis2.entity.MyBank;

/**
* 调用axis2的webservice
* */
public class TestAxis2 {

private static TestAxis2 bean = new TestAxis2();
private String serviceUrl = "http://localhost:8088/bank/services/bankService";// webservice的url(这个url必须是通的,否则调用失败)
private String nameSpace = "http://service.bank.com";// webservice的命名空间(其实就是协议和包名的倒写)

/**
* @see 调用axis2的webservice
* @param method 发布方的方法名
* @param args 方法中的参数列表
* @return MyBank
* */
@SuppressWarnings("unchecked")
public MyBank useAxis2(String method, Object[] args) {
MyBank bank = null;
RPCServiceClient client=null;
try {
client = new RPCServiceClient();
Options option = client.getOptions();
EndpointReference erf = new EndpointReference(serviceUrl);
option.setTo(erf);
QName name = new QName(nameSpace, method);
Class[] returnTypes = new Class[] { MyBank.class };
Object[] response = client.invokeBlocking(name, args, returnTypes);
bank = (MyBank) response[0];
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
client.cleanupTransport();
} catch (AxisFault e) {
e.printStackTrace();
}
}
return bank;
}

public static void main(String[] args) {
MyBank fromBack = new MyBank();
MyBank toBank = new MyBank();
MyBank bank = bean.useAxis2("transition", new Object[]{ fromBack, toBank });
System.out.println(bank.getPrompt());
}

}



运行完毕,控制台没有出现异常,而且打印我想要的结果,
这说明axis2发布和调用webservice都成功了!
fanket2002 2012-08-21
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]

引用 9 楼 的回复:
DESCoder 能否把这个类贴上来?

DESCoder 是一个加密和解密的类代码如下:
public abstract class DESCoder {
/*
* 加密字符串
*/
public static String EncryptAsDoNet(String message, String key) throws Exception {
……
[/Quote]


帅哥 你这个类里面代码是不是没贴完?
String resDe = DESCoder.DecryptDoNet(res,sKey); 这句报错
DecryptDoNet 方法没有啊

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧