简体   繁体   中英

how to authenticate sap web service with ksoap2

I have to connect a SAP web service. Here is my code but it warns:

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

Here is my full code to connect with username and password.

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; 
} 

Try this:

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();
        }
    }

And AuthTransportSE class is:

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());
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM