2021.12.9 java代码对接sap接口(soap协议、webservice)
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                2021.12.9 java代码对接sap接口(soap协议、webservice)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.                        
                                2021.12.9 java對接sap接口(soap協(xié)議、webservice)
問題:對接sap接口,代碼調(diào)試
執(zhí)行:
1、soapui 軟件測試是否能正確訪問
-  未能正確訪問,因為未在本地配置域名映射 
-  要在C:\Windows\System32\drivers\etc里面配置。 - # localhost name resolution is handled within DNS itself.127.0.0.1 localhost # ::1 localhost192.168.2.21 xxx.xxx.com
 
-  配置完后soapui軟件訪問成功! 
2、代碼測試
2.1、用axis框架的方式測試連接
package com.ly.utils; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.axis.encoding.XMLType; import org.apache.axis.message.SOAPHeaderElement; import javax.xml.rpc.ParameterMode; import java.util.List;public class SendWebServiceUtil {/***axis 方式* @param nameSpace 命名空間* @param endpoint wsdl發(fā)布地址* @param methods 方法名* @param list 參數(shù)列表* @return 返回值* @author LY*/public String webserviceConn(String userName,String passWord, String nameSpace, String endpoint, String methods, List<String> list) {String result = null;Object[] parameter = new Object[list.size()];try {Service service = new Service();Call call = (Call) service.createCall();call.setTargetEndpointAddress(new java.net.URL(endpoint));call.setOperationName(new javax.xml.namespace.QName(nameSpace,methods));//WSDL里面描述的接口名稱for(int i=0;i<list.size();i++){call.addParameter("arg"+i, XMLType.XSD_STRING, ParameterMode.IN);//接口的參數(shù)parameter[i] = list.get(i);}call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//設(shè)置返回類型call.setUseSOAPAction(true);call.setSOAPActionURI(nameSpace+methods);//由于需要認(rèn)證,故需要設(shè)置調(diào)用的用戶名和密碼 //String namespace = nameSpace;SOAPHeaderElement header = new SOAPHeaderElement(nameSpace, "AuthHeader");header.addChildElement("Username").setValue(userName);header.addChildElement("Password").setValue(passWord);//call.setUsername(userName);//call.setPassword(passWord);call.addHeader(header);result = (String)call.invoke(parameter);//給方法傳遞參數(shù),并且調(diào)用方法System.out.println("返回結(jié)果:"+result);}catch (Exception e) {System.out.println(e.getMessage());}return result;} } /* * <dependency><groupId>org.apache.axis</groupId><artifactId>axis</artifactId><version>1.4</version></dependency>* <dependency><groupId>javax.xml</groupId><artifactId>jaxrpc-api</artifactId><version>1.1</version></dependency> * * */2.2、用axis2方式的訪問
- package com.ly.webservice;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 org.apache.axis2.transport.http.HTTPConstants; import org.apache.axis2.transport.http.HttpTransportProperties; import javax.xml.namespace.QName;public class Axis2TestConn {public static void main(String[] args) {String[]arrs={"WMS"}; // try { // testConn("http://sappijid.informrack.com:50000/XISOAPAdapter/MessageServlet?senderParty=&senderService=BC_WMS&receiverParty=&receiverService=&interface=SI_IFS007_SYNC_OUT_500&interfaceNamespace=http://informrack.com/ERP/MM", // "urn:sap-com:document:sap:rfc:functions", // "ZYYF01_IFS007",arrs, // "piappljid", // "yf123456"); // } catch (AxisFault axisFault) { // axisFault.printStackTrace(); // }try {testConn("http://localhost:8088/ws-test/hello?wsdl","http://service.ly.com/","sayHello",arrs,"piappljid","yf123456");} catch (AxisFault axisFault) {axisFault.printStackTrace();}}public static void testConn(String serviceName, String namespace, String methodName, Object[]methodArgs, String username, String password) throws AxisFault {Class[] returnTypes = new Class[] { String.class };//返回值類型//RPC方式調(diào)用webserviceRPCServiceClient serviceClient = new RPCServiceClient();Options options = serviceClient.getOptions();HttpTransportProperties.Authenticator basicauth = new HttpTransportProperties.Authenticator();basicauth.setUsername(username); //服務(wù)器訪問用戶名basicauth.setPassword(password); //服務(wù)器訪問密碼options.setProperty(HTTPConstants.AUTHENTICATE, basicauth);//指定調(diào)用的urlEndpointReference targetEPR = new EndpointReference(serviceName);options.setTo(targetEPR);//參數(shù):命名空間、方法名QName op= new QName(namespace,methodName);//調(diào)用遠(yuǎn)程f方法,并指定方法參數(shù)以及返回值類型Object[] response = serviceClient.invokeBlocking(op, methodArgs,returnTypes);for (Object o : response) {System.out.println(o.toString());}//System.out.println(result);} }
2.3、http方式,直接發(fā)送請求報文
import java.io.InputStream;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.UsernamePasswordCredentials;import org.apache.commons.httpclient.auth.AuthScope;import org.apache.commons.httpclient.methods.PostMethod;import org.apache.commons.httpclient.methods.RequestEntity;import org.apache.commons.httpclient.methods.StringRequestEntity;import org.apache.commons.io.IOUtils;public class HttpConnWebServiceUtil {public static void main(String[] args) throws Exception {httpConnSap("你需要訪問的目標(biāo)URL","用戶名","密碼");}/**http 方式直接連接sap* @param endPointAddress webservice發(fā)布地址* @param username 用戶名* @param password 密碼* @Author LY*/public static void httpConnSap(String endPointAddress,String username,String password) throws Exception {// HttpClient發(fā)送SOAP請求int timeout = 10000;System.out.println("HttpClient 發(fā)送SOAP請求");HttpClient client = new HttpClient();//如果需要用戶名密碼驗證;不需要驗證登錄則不需要以下4行UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password);client.getState().setCredentials(AuthScope.ANY, creds);//webservice 地址PostMethod postMethod = new PostMethod(endPointAddress);// 設(shè)置連接超時client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);// 設(shè)置讀取時間超時client.getHttpConnectionManager().getParams().setSoTimeout(timeout);// 然后把Soap請求數(shù)據(jù)添加到PostMethod中RequestEntity requestEntity = new StringRequestEntity(getXML(),"text/xml", "UTF-8");// 設(shè)置請求頭部,否則可能會報 “no SOAPAction header” 的錯誤postMethod.setRequestHeader("SOAPAction", "");// 設(shè)置請求體postMethod.setRequestEntity(requestEntity);int status = client.executeMethod(postMethod);if (status == 200) {// 成功InputStream is = postMethod.getResponseBodyAsStream();// 獲取請求結(jié)果字符串String result = IOUtils.toString(is);System.out.println("請求成功!"+"\n"+"返回結(jié)果:"+result);} else {System.out.println("請求失敗!"+"\n"+"錯誤代碼:"+status+"\n"+"返回報文:"+"\n"+postMethod.getResponseBodyAsString());}}/*** @return 獲取請求報文 這里我是直接從soapui 里面測試再把請求報文復(fù)制過來的。*/public static String getXML(){String soapXML="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:sap-com:document:sap:rfc:functions\">\n" +" <soapenv:Header/>\n" +" <soapenv:Body>\n" +" <urn:ZYYF01_IFS007>\n" +" <IS_INPUT>\n" +"<DEST>\n" +"\n" +"<DEST_ID>WMS</DEST_ID>\n" +"\n" +"<BUSS_TP>ZYYF01_IFS007</BUSS_TP>\n" +"\n" +"</DEST>\n" +"\n" +"\n" +"<DATA>\n" +"\n" +"<AUFNR>000100118369</AUFNR>\n" +"\n" +"<ZUECK>RP21112415372546</ZUECK>\n" +"\n" +"<VORNR>0012</VORNR>\n" +"\n" +"<LMNGA>100.0</LMNGA>\n" +"\n" +"<BUDAT>2021-11-24</BUDAT>\n" +"\n" +"<VGW01>1669.9</VGW01>\n" +"\n" +"<VGE01>KG</VGE01>\n" +"\n" +"<VGW02>1669.9</VGW02>\n" +"\n" +"<VGE02>KG</VGE02>\n" +"\n" +"<VGW03>1669.9</VGW03>\n" +"\n" +"<VGE03>KG</VGE03>\n" +"\n" +"<VGW04>1669.9</VGW04>\n" +"\n" +"<VGE04>KG</VGE04>\n" +"\n" +"<ZZBGZL>1669.9</ZZBGZL>\n" +"\n" +"<ZZCLASS>229A1</ZZCLASS>\n" +"\n" +"<EXT02>EA</EXT02>\n" +"\n" +"</DATA>\n" +"\n" +"\n" +" </IS_INPUT>\n" +" </urn:ZYYF01_IFS007>\n" +" </soapenv:Body>\n" +"</soapenv:Envelope>";return soapXML;}}- 最后,經(jīng)過測試還是選擇了這種方式。
3、問題遺留
2.1和2.2的方式,我訪問本地發(fā)布的webservice沒有問題,但是訪問別人的就有問題,求解答。2.3的方式都可以訪問
可能原因,就是請求報文的問題
測試了一下,是請求報文的問題,訪問網(wǎng)址自動生成的報文去請求 報500錯誤,要使用特定的報文,所以還是要拼接報文,2,.1和2.2如何拼接報文?(是否有必要)
注釋一些報文后就請求成功了。
4、相關(guān)工具
soapui(接口測試工具)下載地址(一直滑到最下面找到普通下載,不然選擇其他的會給你安裝一些流氓軟件)
https://www.zdfans.com/html/18711.html
總結(jié)
以上是生活随笔為你收集整理的2021.12.9 java代码对接sap接口(soap协议、webservice)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: Python地理数据处理 六:使用OGR
- 下一篇: 利用logic friday 把分组密码
