Java 使用Axis調(diào)用WebService的示例代碼
更新時間:2020年09月28日 09:47:19 作者:H.U.C-王子
這篇文章主要介紹了Java 使用Axis調(diào)用WebService的示例代碼,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
import org.apache.axis.client.Call; import org.apache.axis.client.Service; /** * @ClassName: TestAxis * @Description: TODO(描述這個類的作用) * @author huc * */ public class TestAxis { public static void main(String []args){ String inConditions = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><ROWS><INFO><SBM>*</SBM></INFO><ROW><GMSFHM>公民身份號碼</GMSFHM><XM>姓名</XM></ROW><ROW><GMSFHM>110101******</GMSFHM><XM>李聞</XM><FSD>100600</FSD><YWLX>個人貸款</YWLX></ROW><ROW><GMSFHM>3624221952123***</GMSFHM><XM>李一聞</XM><FSD>100600</FSD><YWLX>個人貸款</YWLX></ROW><ROW><GMSFHM>1234********</GMSFHM><XM>王龍</XM><FSD>100600</FSD><YWLX>銀行開戶</YWLX></ROW><ROW><GMSFHM>110101******</GMSFHM><XM></XM><FSD>100600</FSD><YWLX>個人車貸</YWLX></ROW><ROW><GMSFHM>110101******</GMSFHM><XM></XM><FSD>100600</FSD><YWLX></YWLX></ROW><ROW><GMSFHM>230602***</GMSFHM><XM></XM><FSD>100600</FSD><YWLX>個人車貸</YWLX></ROW></ROWS>"; String inLicense = "********"; try{ //調(diào)用webservice地址 String url = "https://www.****.com/services/NciicServices"; //如果url地址中有中文參數(shù),要注意應單獨將中文部分進行編碼操作后再與URL字符串拼接到一起,編碼方式為:URLEncoder.encode("中文部分", "utf-8"); //調(diào)用方法名 String method="nciicCheck"; Service service = new Service(); //通過service創(chuàng)建call對象 Call call = (Call) service.createCall(); //call.setSOAPVersion(SOAPConstants.SOAP12_CONSTANTS); 設置soap12協(xié)議方式調(diào)用 //設置服務地址 call.setTargetEndpointAddress(new java.net.URL(url)); //設置調(diào)用方法 call.setOperationName(method); call.setUseSOAPAction(true); //添加方法的參數(shù),有幾個添加幾個 //inLicense是參數(shù)名,XSD_STRING是參數(shù)類型,IN代表傳入 call.addParameter("inLicense", org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN); call.addParameter("inConditions", org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN); //帶命名空間的寫法 //call.setOperationName(new QName("http://bussiness.***.com", "callPT")); //call.addParameter(new QName("http://bussiness.***.com","xmlData"),org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN); //call.addParameter(new QName("http://bussiness.***.com","methodName"),org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN); //設置返回類型 call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING); //解決錯誤:服務器未能識別 HTTP 頭 SOAPAction 的值 call.setUseSOAPAction(true); call.setSOAPActionURI(targetNamespace + operationName); Object ret= null; try{ //使用invoke調(diào)用方法,Object數(shù)據(jù)放傳入的參數(shù)值 ret = call.invoke(new Object[] {inLicense,inConditions}); }catch(Exception e){ e.printStackTrace(); } //輸出SOAP請求報文 System.out.println("--SOAP Request: " + call.getMessageContext().getRequestMessage().getSOAPPartAsString()); //輸出SOAP返回報文 System.out.println("--SOAP Response: " + call.getResponseMessage().getSOAPPartAsString()); //輸出返回信息 System.out.println("result==="+ret.toString()); }catch(Exception e){ e.printStackTrace(); } } }
下面是輸出結果信息:
--SOAP Request: <?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><nciicCheck soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><inLicense xsi:type="xsd:string">*****</inLicense><inConditions xsi:type="xsd:string">*****</inConditions></nciicCheck></soapenv:Body></soapenv:Envelope> --SOAP Response: <?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soap:Body><ns1:nciicCheckResponse xmlns:ns1="https://api.nciic.org.cn/NciicServices"><ns1:out><?xml version="1.0" encoding="UTF-8"?> <RESPONSE errorcode="-72" code="0" countrows="1"><ROWS><ROW><ErrorCode>-72</ErrorCode><ErrorMsg>IP地址受限</ErrorMsg></ROW></ROWS></RESPONSE></ns1:out></ns1:nciicCheckResponse></soap:Body></soap:Envelope> result===<?xml version="1.0" encoding="UTF-8"?> <RESPONSE errorcode="-72" code="0" countrows="1"><ROWS><ROW><ErrorCode>-72</ErrorCode><ErrorMsg>IP地址受限</ErrorMsg></ROW></ROWS></RESPONSE>
以上就是Java 使用Axis調(diào)用WebService的示例代碼的詳細內(nèi)容,更多關于Java 使用Axis調(diào)用WebService的資料請關注腳本之家其它相關文章!
相關文章
Java集合Map常見問題_動力節(jié)點Java學院整理
這篇文章主要為大家詳細整理了Java集合Map常見問題,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05SpringBoot集成elasticsearch使用圖文詳解
Spring Boot集成Elasticsearch其實非常簡單,這篇文章主要給大家介紹了關于SpringBoot集成elasticsearch使用的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-04-04Java實現(xiàn)多個sheet頁數(shù)據(jù)導出功能
這篇文章主要為大家詳細介紹了Java實現(xiàn)多個sheet頁數(shù)據(jù)導出功能的相關知識,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2024-03-03