java - 如何使用 ksoap2 验证 sap web 服务

标签 java android web-services sap ksoap2

我必须连接 SAP 网络服务。这是我的代码,但它警告:

org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope (position:START_TAG @1:7 in java.io.InputStreamReader@40e3fe98)

这是我使用用户名和密码连接的完整代码。

final static String NAMESPACE = "urn:sap-com:document:sap:soap:functions:mc-style";
final static String METHOD_NAME = "ZmblHucremalzemelistesi";
final static String SOAP_ACTION = "";
final static String URL = "http://xxx.xxx.xxx.xxx:1080/sap/bc/srt/wsdl/bndg_4F969242EA785040E10080008D0B0B03/wsdl11/allinone/ws_policy/document?sap-client=010";


private void testWS() {
    // TODO Auto-generated method stub


    SoapObject reSoapObject = new SoapObject(NAMESPACE, METHOD_NAME);
    SoapSerializationEnvelope soaSerializationEnvelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);

    reSoapObject.addProperty("ILgort", "H12");

    soaSerializationEnvelope.setOutputSoapObject(reSoapObject);
    soaSerializationEnvelope.headerOut = new Element[1]; 
    soaSerializationEnvelope.headerOut[0] = buildAuthHeader(); 

    HttpTransportSE httpTransportSE = new HttpTransportSE(URL);

    try {

        httpTransportSE.call(SOAP_ACTION, soaSerializationEnvelope);

        Object response = soaSerializationEnvelope.getResponse();

        tv.setText(response.toString());

    } catch (IOException e) {
        e.printStackTrace();
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    }


}

private Element buildAuthHeader() { 

    Element h = new Element().createElement(NAMESPACE, "AuthHeader"); 
    Element username = new Element().createElement(NAMESPACE, "user"); 
    username.addChild(Node.TEXT, "testuser"); 
    h.addChild(Node.ELEMENT, username); 
    Element pass = new Element().createElement(NAMESPACE, "pass"); 
    pass.addChild(Node.TEXT, "testpwd"); 
    h.addChild(Node.ELEMENT, pass); 

    return h; 
} 

最佳答案

试试这个:

final static String NAMESPACE = "urn:sap-com:document:sap:soap:functions:mc-style";
    final static String METHOD_NAME = "ZmblHucremalzemelistesi";
    final static String SOAP_ACTION = "";
    final static String URL = "http://xxx.xxx.xxx.xxx:1080/sap/bc/srt/wsdl/bndg_4F969242EA785040E10080008D0B0B03/wsdl11/allinone/ws_policy/document?sap-client=010";

    private static final String USERNAME = "YOUR_USERNAME";
    private static final String PASSWORD = "YOUR_PASSWORD";

    private void testWS() {
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        request.addProperty("ILgort", "H12");

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.setOutputSoapObject(request);

        AuthTransportSE androidHttpTransport = new AuthTransportSE(URL, USERNAME, PASSWORD);
        androidHttpTransport.debug = true;

        try {
            androidHttpTransport.call(SOAP_ACTION, envelope);
            SoapObject response = (SoapObject) envelope.getResponse();
            // if it did not work, try this:
            // SoapObject response = (SoapObject) envelope.bodyIn;

            tv.setText(response.toString());

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

AuthTransportSE 类是:

import java.io.IOException;

import org.ksoap2.transport.HttpTransportSE;
import org.ksoap2.transport.ServiceConnection;
import org.ksoap2.transport.ServiceConnectionSE;

public class AuthTransportSE extends HttpTransportSE{
    private String username;
    private String password;

    public AuthTransportSE(String url, String username, String password) {
        super(url);
        this.username = username;
        this.password = password;       
    }

    protected ServiceConnection getServiceConnection() throws IOException {
        ServiceConnection midpConnection = new ServiceConnectionSE(url);
        addBasicAuthentication(midpConnection);
        return midpConnection;
    }

    protected void addBasicAuthentication(ServiceConnection midpConnection) throws IOException {
        if (username != null && password != null) {
            StringBuffer buf = new StringBuffer(username);
            buf.append(':').append(password);
            byte[] raw = buf.toString().getBytes();
            buf.setLength(0);
            buf.append("Basic ");
            org.kobjects.base64.Base64.encode(raw, 0, raw.length, buf);
            midpConnection.setRequestProperty("Authorization", buf.toString());
        }
    }
}

关于java - 如何使用 ksoap2 验证 sap web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10316689/

相关文章:

javascript - 如何使用在 JQuery 中完美运行的 Angular 来解决这个问题?

java - 当池中仍有线程运行时,为什么 executor.isShutdown() 返回 true?

java - 像MS Paint一样画直线

java - 在eclipse中为Android App创建新项目时出现很多错误

java - 如何将较暗的透明度添加到我的相对布局中,如图所示?

安卓动态壁纸幻灯片动画

android - Ionic click/tap 事件不会触发外部 URL

java - Android模拟器无法加载应用程序

java - 使用 chrome 远程运行 selenium 测试用例下载文件测试

java - CommandLineRunner 和 Springboot 与 Hibernate 的问题 - 无法初始化代理 session - 无 session