使用适用于 Android 的 KSOAP 进行基本 HTTP 身份验证

发布于 2024-10-20 05:24:22 字数 2312 浏览 4 评论 0原文

我需要使用 Android 使用 SOAP Web 服务。

问题是,在请求特定功能之前,我需要使用基本的 http 请求对客户端进行身份验证。

您知道如何使用 KSOAP 来做到这一点吗?

到目前为止,我已经尝试使用 httpsTransportSE.call() 的重载方法,因为它建议我可以为 http 连接指定附加标头

(参考: 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”是base 64编码的字符串“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());
        }

但我收到了消息“permission returned”的报告。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

油焖大侠 2024-10-27 05:24:23

试试这个。这对我有用,从 Android 应用程序使用 .Net 服务。我使用过ksoap2-android-assemble-2.5.8-jar-with-dependency.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();
}

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();
}
<逆流佳人身旁 2024-10-27 05:24:23

在与此相关的问题中, HeaderProperty 的第二个参数中没有冒号 - 它将是“Basic dXNlcjpwYXNz” - 也许这就是问题所在? (哪一个是正确的?)

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

酷到爆炸 2024-10-27 05:24:23

请参阅此内容。我已经实现了一个从 Android 应用程序调用本地 SOAP Web 服务(.asmx)的简单代码。

这是一个简单的 Web 服务,使用用户名和密码对用户进行身份验证。

希望有帮助

干杯

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

日裸衫吸 2024-10-27 05:24:23

您很可能需要在 Android 清单文件中包含以下内容:

<uses-permission android:name="android.permission.INTERNET" />

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

<uses-permission android:name="android.permission.INTERNET" />
海拔太高太耀眼 2024-10-27 05:24:23

您尝试过 HttpTranspostBasicAuth 吗?

以下是参考信息:
http://ksoap2.sourceforge.net/doc/api/org /ksoap2/transport/HttpTransportBasicAuth.html

Have you tried HttpTranspostBasicAuth?

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

愿得七秒忆 2024-10-27 05:24:23

仅限此工作 https://stackoverflow.com/a/5614243/7198554
....

soapEnvelope.headerOut = new Element[1];

这个-> SoapSerializationEnvelope 信封
命名空间这个 -> http://tempuri.org/
我的代码=>

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;

在 VS C# 中

public class AuthHeader : SoapHeader
{
public string Username;
//public string Password;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文