Basic HTTP authentication using KSOAP for android

前端 未结 6 1731
暖寄归人
暖寄归人 2020-12-09 23:01

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 reque

相关标签:
6条回答
  • 2020-12-09 23:12

    THIS WORK only https://stackoverflow.com/a/5614243/7198554 ....

    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;
    }
    
    0 讨论(0)
  • 2020-12-09 23:19

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

    <uses-permission android:name="android.permission.INTERNET" />
    
    0 讨论(0)
  • 2020-12-09 23:22

    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();
         }
    
    0 讨论(0)
  • 2020-12-09 23:22

    Have you tried HttpTranspostBasicAuth?

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

    0 讨论(0)
  • 2020-12-09 23:26

    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

    0 讨论(0)
  • 2020-12-09 23:30

    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?)

    0 讨论(0)
提交回复
热议问题