Focus on IT Recommend

Home > android - Accessing HTTP authentificated SOAP webservice via KSOAP2 HttpTransportBasicAuth

android - Accessing HTTP authentificated SOAP webservice via KSOAP2 HttpTransportBasicAuth

I need to connect to webservice, which requers authorization. I tried to use headers:

Element usernameElement = new Element().createElement(NAMESPACE, "Username");
                       usernameElement.addChild(Node.TEXT, "user");
                       Element passwordElement = new Element().createElement(NAMESPACE,   "Password");
                        passwordElement.addChild(Node.TEXT, "pass");
                        Element header = new Element().createElement(NAMESPACE, "AuthHeader");
                        header.addChild(Node.ELEMENT, usernameElement);
                        header.addChild(Node.ELEMENT, passwordElement);

soapEnvelope.headerOut= new Element[]{header};

I receive the error: 07-25 14:03:20.922: WARN/System.err(584): org.xmlpull.v1.XmlPullParserException: expected: START_TAG {}Envelope (position:START_TAG @1:6 in

I understand, webservice is returning some HTML formatted error, but how can i see what is it? Can i make textview.settext() of such a request or print it to LogCat? How can i make it and Why am i receiving it?:(

Then, i tried to use HttpTransportBasicAuth class - faced a lot of problems to make it work - i had to add it to the project, change manually its extension from HttpTransport to HttpTransportSE and from ServiceConnectionMIDP to ServiceConnectionSE because there wasn't such classes in ksoap2-android-assembly-2.5.7. At last it compiled without errors:


import org.ksoap2.transport.*;

import org.ksoap2.transport.HttpTransportSE;

public class HttpTransportBasicAuth extends HttpTransportSE {
     private String username;   
        private String password;   
         * Constructor with username and password  
         * @param url  
         *            The url address of the webservice endpoint  
         * @param username  
         *            Username for the Basic Authentication challenge RFC 2617   
         * @param password  
         *            Password for the Basic Authentication challenge RFC 2617  
        public HttpTransportBasicAuth(String url, String username, String password) {   
        this.username = username;   
        this.password = password;   

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

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

} it is ok now with the method, as there is no an error, but i'm still receiving the error: 07-25 14:03:20.922: WARN/System.err(584): org.xmlpull.v1.XmlPullParserException: expected: START_TAG {}Envelope (position:START_TAG @1:6 in when i run project. Here's my code by which i try to connect to webservice:

  HttpTransportBasicAuth aht= new HttpTransportBasicAuth(URL, "user", "pass");

  , soapEnvelope);

            SoapPrimitive tmp_ResultString = (SoapPrimitive) soapEnvelope.getResponse();
            ResultString = tmp_ResultString.toString();
         // tv.setText(ResultString);
          catch(Exception e)


And finally, i tried to use this source: Webservice Http Authentication - KSOAP2 on Android but compiler do not know what HeaderProperty is. What should i import to be it ok? Please answer how can i see what the exact error message webservice returns in tag, because of what i'm receiving the error in LogCat and please answer if i'm doing anything wrong while trying to make it work. Thanks.

Recommend:web services - Can't send object of ComplexType to soap webservice from ksoap2 for android

to send complex objects through the webservice that contained String, doubles, int, etc... Then i was capable of send an array of bytes through the network. And now comes my problem: When i try to create an object to send over the network

android web-services ksoap2 http-authentication
  this question
asked Jul 25 '11 at 14:29 Keem 11 1 3


1 Answers

You need to use ksoap2-Android 2.5.7 (

There you can find the class HeaderProperty. The link you show works for me at least.

  this answer
answered Sep 8 '11 at 14:27 idiaz 21 1 5


Recommend:web services - Webservice Http Authentication - KSOAP2 on Android

tion for my problem. This article does not really help either, because the classes and methods used are not present in ksoap2 for android: Basic HTTP authentication using KSOAP for android I am trying to access a SAP webservice, which I set


------splitte line----------------------------