title: Accessing HTTP authenticated SOAP webservice via KSOAP2 HttpTransportBasicAuth
body: <p>I need to connect to webservice, which requers authorization.
  I tried to use headers:</p>
  
  <pre><code>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};
  </code></pre>
  
  <p>I receive the error:
  07-25 14:03:20.922: WARN/System.err(584): org.xmlpull.v1.XmlPullParserException: expected: START_TAG {<a href="http://schemas.xmlsoap.org/soap/envelope/" rel="nofollow noreferrer">http://schemas.xmlsoap.org/soap/envelope/</a>}Envelope (position:START_TAG @1:6 in java.io.InputStreamReader@44f632f8) </p>
  
  <p>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?:(</p>
  
  <p>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:</p>
  
  <pre><code>package com.android.testinet;
  
  import org.ksoap2.transport.*;
  
  import org.ksoap2.transport.HttpTransportSE;
  import java.io.*;  
  
  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) {   
          super(url);   
          this.username = username;   
          this.password = password;   
      }   
  
      protected ServiceConnection getServiceConnection() throws IOException {   
          ServiceConnectionSE midpConnection = new ServiceConnectionSE(url);   
          addBasicAuthentication(midpConnection);   
          return midpConnection;   
      }   
  
      protected void addBasicAuthentication(ServiceConnection midpConnection) throws IOException {   
          if (username != null &amp;&amp; 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());   
          }   
      }   
  </code></pre>
  
  <p>}
  it is ok now with the HttpTransportBasicAuth.call 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 {<a href="http://schemas.xmlsoap.org/soap/envelope/" rel="nofollow noreferrer">http://schemas.xmlsoap.org/soap/envelope/</a>}Envelope (position:START_TAG @1:6 in java.io.InputStreamReader@44f632f8) when i run project.
  Here's my code by which i try to connect to webservice:</p>
  
  <pre><code>  HttpTransportBasicAuth aht= new HttpTransportBasicAuth(URL, "user", "pass");
            try
            {
  
              aht.call(SOAP_ACTION, soapEnvelope);
  
              SoapPrimitive tmp_ResultString = (SoapPrimitive) soapEnvelope.getResponse();
              ResultString = tmp_ResultString.toString();
           // tv.setText(ResultString);
            }
            catch(Exception e)
            {
              e.printStackTrace();
  
            }
  </code></pre>
  
  <p>And finally, i tried to use this source:
  <a href="https://stackoverflow.com/questions/5501743/webservice-http-authentication-ksoap2-on-android">Webservice Http Authentication - KSOAP2 on Android</a>
  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 <code>&lt;html&gt;</code> 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.</p>