Basic HTTP authentication using KSOAP for android
Asked Answered
C

6

8

I need to consume a SOAP web service using Android.

The issue is that before request for a particular function I need to authenticate a client using basic http request.

Do you know how to do this using KSOAP?

Until this moment I have tried using overloaded method of httpsTransportSE.call() as it suggest that I can specify additional headers for http connection

(ref: https://github.com/mosabua/ksoap2-android/blob/master/ksoap2-j2se/src/main/java/org/ksoap2/transport/HttpTransportSE.java)

headerPropertyList.add(new HeaderProperty("Authorization", "Basic : dXNlcjpwYXNz"));

"cdXNlcjpwYXNz" is base 64 encoded string of "user:pass"

 public List call(String soapAction, SoapEnvelope envelope, List headers)
    * @param headers a list of HeaderProperties to be http header properties when establishing the connection



private static final String SOAP_ACTION = "someaddress/IPortReporting/GetPortStatus";
private static final String METHOD_NAME = "methodname";
private static final String NAMESPACE = "http://ssn.someaddress/2.0/";
private static final String URL = "new.someaddress/functionName.svc";




SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
        Request.addProperty("MessageId", "1");

        SoapSerializationEnvelope soapEnvelop = new SoapSerializationEnvelope(
                11);
        //soapEnvelop.headerOut = addHeaders(); 
        soapEnvelop.dotNet = true;
        soapEnvelop.setOutputSoapObject(Request);


        List<HeaderProperty> headerPropertieList = new ArrayList<HeaderProperty>();
        headerPropertyList.add(new HeaderProperty("Authorization", "Basic : cG9ydHdzOjEyM3F3ZUFTRA=="));
        //HeaderProperty headerProperty = new HeaderProperty()

        HttpsTransportSE httpsse = new HttpsTransportSE(URL, 443, "", 5000);



        try {
            httpsse.call(SOAP_ACTION, soapEnvelop, headerPropertyList);
            //httpsse.call(SOAP_ACTION, soapEnvelop);

            SoapPrimitive resultString = (SoapPrimitive) soapEnvelop
                    .getResponse();
            tv.setText("Status: ");
        } catch (Exception e) {
            tv.setText("Some error," + " "
                    + e.getMessage());
        }

But I have message "permission denied" reported back.

Cliffordclift answered 3/3, 2011 at 7:41 Comment(1)
when you instantiate headerPropertyList you write property, but later when you set it, you call it propertie could that be?Ideograph
O
14

Try this. This worked for me, consuming .Net service from Android application. I have used ksoap2-android-assembly-2.5.8-jar-with-dependencies.jar

String NAMESPACE = "http://www.namespace.com/";
String METHOD_NAME = "MethodName";
String SOAP_ACTION = "http://www.namespace.com/MethodName";
String URL = "https://www.namespace.com/services/Service.asmx";

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
// Set all input params   
request.addProperty("property", "value");   
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
// Enable the below property if consuming .Net service
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
 
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {

    List<HeaderProperty> headerList = new ArrayList<HeaderProperty>();
    headerList.add(new HeaderProperty("Authorization", "Basic " + 
        org.kobjects.base64.Base64.encode("username:password".getBytes())));

    androidHttpTransport.call(SOAP_ACTION, envelope, headerList);
    SoapObject response = (SoapObject)envelope.getResponse();
    //response.getProperty(0).toString();

} catch(Exception e) {
     e.printStackTrace();
}
Oujda answered 27/9, 2011 at 22:29 Comment(2)
how to add username and password in above exampleUpon
get Malformed input: Mixed contentSakovich
F
4

At the linked question to this one, the second arg of the HeaderProperty does not have a colon in it - it would be "Basic dXNlcjpwYXNz" - maybe that's the problem? (Which one is correct?)

Faithfaithful answered 1/6, 2011 at 18:0 Comment(1)
I confirmed it - definitely no colon should be in the "Basic..." string.Faithfaithful
G
2

You most probably need to include the following in your Android manifest file:

<uses-permission android:name="android.permission.INTERNET" />
Grecism answered 20/3, 2011 at 17:6 Comment(1)
<uses-permission android:name="android.permission.INTERNET" />Grecism
W
2

See this. I have implemented a simple code that calls a local SOAP web service(.asmx) from Android application.

It is a simple web service to authenticate user using username and password.

Hope that helps

Cheers

Wireless answered 24/11, 2011 at 4:15 Comment(0)
A
1

Have you tried HttpTranspostBasicAuth?

Here is the reference information: http://ksoap2.sourceforge.net/doc/api/org/ksoap2/transport/HttpTransportBasicAuth.html

Akan answered 13/7, 2011 at 18:26 Comment(1)
That class relies on J2ME and therefore does not work on Android.Ogpu
M
0

THIS WORK only https://mcmap.net/q/717401/-how-to-set-soap-header-using-ksoap2-android ....

soapEnvelope.headerOut = new Element[1];

this - > SoapSerializationEnvelope envelope NAMESPACE this -> http://tempuri.org/ my code=>

public Element buildAuthHeader() {
Element h = new Element().createElement(NAMESPACE, "AuthHeader");
Element username = new Element().createElement(NAMESPACE, "Username");
username.addChild(Node.TEXT, "test");
h.addChild(Node.ELEMENT, username);
// Element pass = new Element().createElement(NAMESPACE, "pass");
// pass.addChild(Node.TEXT, pass);
// h.addChild(Node.ELEMENT, pass);
return h;

In VS C#

public class AuthHeader : SoapHeader
{
public string Username;
//public string Password;
}
Matthieu answered 23/11, 2016 at 7:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.