。Net Web服务中的Android-Ksoap2身份验证

问题描述 投票:1回答:1

我有一个使用.so网络服务的android应用程序,我正在使用kso​​ap2。 我想传递凭据(用户名和密码),所以我需要使用一种身份验证类型(窗体,基本,...),但我不知道如何。

我从以下位置下载了安全的Web服务(此示例要求提供凭据):Web Services Security

[WebMethod]
public string HelloWorld()
{
    return "Hello " + Context.User.Identity.Name;
}

我用基本代码调用webMethod,这就是为什么我会收到错误消息,我需要传递用户名和密码的地方,但是我不知道如何。有相关的链接,这些链接使用NTCredentials,envelope.headerOut和HeaderProperty(我没有的类),我尝试了所有尝试而没有尝试。

public String getStringWebService()
{
    String namespaceServicio;
    String nombreMetodo;

    namespaceServicio = "http://tempuri.org/";              
    nombreMetodo = "HelloWorld";        
    accionSoap = namespaceServicio + nombreMetodo;

    urlServicio = "http://???.???.???/Prueba/Autenticacion/AuthTestSvc/Service1.asmx";

    SoapObject request = new SoapObject(namespaceServicio, nombreMetodo);                       
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);
    envelope.bodyOut = request; 
    envelope.dotNet = true;
    envelope.encodingStyle = SoapEnvelope.XSD;

    HttpTransportSE ht = new HttpTransportSE(urlServicio);

    ht.debug = true;
    String cad = "";
    try
    {
       String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
         " <soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
         " <soap:Header>" +
         " <AUTHHEADER xmlns=\"http://tempuri.org/\" />" +
         " <username>test</username> " +
         " <password>test</password> " +
         " </AUTHHEADER> " + 
         " </soap:Header> " +
         " <soap:Body> " +
         " <SENSITIVEDATA xmlns=\"http://tempuri.org/\"> " +
         " </soap:Body> " + 
         " </soap:Envelope> ";
       ht.setXmlVersionTag(xml);
       ht.call(accionSoap, envelope);       
       cad = ht.responseDump;          
       return cad;
    }
    catch(Exception e)
    {
       throw new Exception(e.toString());
    }
}

异常发生在httpTransportSE.call(soapAction,信封)中,因为我说我没有传递用户名和密码

org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/envelope/}Envelope (position:START_TAG <html>@1:6 in java.io.InputStreamReader@44f13428)

在ht.responseDump中,我得到html代码:

401 - Unauthorized: Access is denied due to invalid credentials.
You do not have permission to view this directory or page using the credentials that you supplied
android android-ksoap2
1个回答
0
投票

This is a wonderful tutorial向您展示如何一起使用Ksoap和.net Web服务。作者逐步编写代码(例如肥皂对象,信封,然后运输等)。

祝你好运。>>

© www.soinside.com 2019 - 2024. All rights reserved.