关于java访问别人的webservice接口问题
最近要做一个在java中调用远程webservice接口,根据接口中要提供的参数和方法来调用,我现在用AXIS调用远程的web service的方式来实现,代码如下:
String serviceurl="http://ajcx.hbfy.gov.cn/service/dataService.ws?wsdl";
String namespace = "http://ws.tdh/";
String methodName = "GetAjInfo";
String soapActionURI = "http://ws.tdh/GetAjInfo";
QName qn = new QName("http://ws.tdh/",
"GetAjInfo");
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new URL(serviceurl));
call.setUseSOAPAction(true);
call.setSOAPActionURI(soapActionURI);
call.setOperationName(new QName(namespace, methodName));//
call.addParameter("Userid", org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);// param
call.addParameter("Pwd", org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);// param
call.addParameter("RequestXML", org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);// param
String uname= ""; //用户名 param1
String pwd=""; //密码 param1
//以下字段逐个base64编码
String fydm="H10";
byte[] fydmByte= fydm.getBytes("UTF-8");
String fydmEcodeStr=Base64.encode(fydmByte);
String ah="(2013)鄂武汉中知初字第02670号";
byte[] ahByte= ah.getBytes("UTF-8");
String ahEcodeStr=Base64.encode(ahByte);
String ajmm="250304";
byte[] ajmmByte= ajmm.getBytes("UTF-8");
String ajmmEcodeStr=Base64.encode(ajmmByte);
String requestxml="<Request><FYDM>"+fydmEcodeStr+"</FYDM> <AH>"+ahEcodeStr+"</AH><AJMM>"+ajmmEcodeStr+"</AJMM></Rquest>";
byte[] requestxmlBate= requestxml.getBytes("UTF-8");
String encodeStr=Base64.encode(requestxmlBate); //整体 base64编码 param1
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
// byte[] cityByte= city.getBytes("UTF-8");
//String citycodeStr=Base64.encode(cityByte);
//call.invoke(new Object[]{city});
//call.invoke(new Object[]{null,null,encodeStr});
//call.invoke(new Object[]{"","",encodeStr});
String result=(String)call.invoke(new Object[]{"","",encodeStr});
System.out.println("result is "+result);
/* for(String r:result){
System.out.println("r======:"+r);
}*/
}
catch (Exception e) {
System.err.println(e.toString());
}
运行测试遇到一个问题很纠结,控制台输出: String serviceurl="http://ajcx.hbfy.gov.cn/service/dataService.ws?wsdl";
String namespace = "http://ws.tdh/";
String methodName = "GetAjInfo";
String soapActionURI = "http://ws.tdh/GetAjInfo";
QName qn = new QName("http://ws.tdh/",
"GetAjInfo");
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new URL(serviceurl));
call.setUseSOAPAction(true);
call.setSOAPActionURI(soapActionURI);
call.setOperationName(new QName(namespace, methodName));//
call.addParameter("Userid", org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);// param
call.addParameter("Pwd", org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);// param
call.addParameter("RequestXML", org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);// param
String uname= ""; //用户名 param1
String pwd=""; //密码 param1
//以下字段逐个base64编码
String fydm="H10";
byte[] fydmByte= fydm.getBytes("UTF-8");
String fydmEcodeStr=Base64.encode(fydmByte);
String ah="(2013)鄂武汉中知初字第02670号";
byte[] ahByte= ah.getBytes("UTF-8");
String ahEcodeStr=Base64.encode(ahByte);
String ajmm="250304";
byte[] ajmmByte= ajmm.getBytes("UTF-8");
String ajmmEcodeStr=Base64.encode(ajmmByte);
String requestxml="<Request><FYDM>"+fydmEcodeStr+"</FYDM> <AH>"+ahEcodeStr+"</AH><AJMM>"+ajmmEcodeStr+"</AJMM></Rquest>";
byte[] requestxmlBate= requestxml.getBytes("UTF-8");
String encodeStr=Base64.encode(requestxmlBate); //整体 base64编码 param1
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
// byte[] cityByte= city.getBytes("UTF-8");
//String citycodeStr=Base64.encode(cityByte);
//call.invoke(new Object[]{city});
//call.invoke(new Object[]{null,null,encodeStr});
//call.invoke(new Object[]{"","",encodeStr});
String result=(String)call.invoke(new Object[]{"","",encodeStr});
System.out.println("result is "+result);
/* for(String r:result){
System.out.println("r======:"+r);
}*/
}
catch (Exception e) {
System.err.println(e.toString());
}
遇到一个问题,控制台输出异常信息:Fault occurred while processing,烦劳有人知道的帮忙看看是什么原因,急求答案,谢谢了
[解决办法]
wsdl用得比较少,json用得较多。。
[解决办法]
java 调用webservice抛异常问题,请参考http://blog.csdn.net/baohuan_love/article/details/14449559
[解决办法]
我发了个空串
返回
<?xml version="1.0" encoding="utf-8" ?><Response><Result><Code>Mg==</Code><Msg>5Y+C5pWwUmVxdWVzdFhNTOaXoOivt+axguWPguaVsA==</Msg></Result></Response>
按照Base64翻译过来就是:参数RequestXML无请求参数
说明那边还能运行,那就是你发送的请求格式不对或者编码不对
[解决办法]
我用jax-ws得到是这么个东西
<?xml version="1.0" encoding="utf-8" ?><Response><Result><Code>1</Code><Msg></Msg></Result><Data Count="1"><AJ><AHDM>225100000149189</AHDM><AH>(2013)鄂武汉中知初字第02670号%</AH><AJLX>民A1</AJLX><LARQ>2013-07-29</LARQ><JARQ>2013-10-28</JARQ><AYMS>侵害商标权纠纷</AYMS><DSRC>原告:烟台三环锁业集团有限公司;被告:王烈</DSRC><CBBMMC>知识产权庭</CBBMMC><CBRRXM>魏大海</CBRRXM><AJZT>结案</AJZT><KTRQ>2013-10-09 02:30:00</KTRQ><KTFT>第二十四法庭(6人)</KTFT></AJ></Data></Response>