【问题标题】:Android - Ksoap2 Authentication in .Net Web ServiceAndroid - .Net Web 服务中的 Ksoap2 身份验证
【发布时间】:2011-10-28 05:27:20
【问题描述】:

我有一个使用 .Net 网络服务的 android 应用程序,因为我正在使用 ksoap2。 我想传递凭据(用户名和密码),所以我需要使用一种身份验证(表单、基本...),但我不知道如何。

我从以下位置下载了一个安全的 WebService(此示例要求提供凭据): 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, envelope) 中,因为正如我所说,我没有传递用户名和密码

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

【问题讨论】:

  • 请给我们看代码和logcat。

标签: android android-ksoap2


【解决方案1】:

This is a wonderful tutorial 向您展示如何同时使用 Ksoap 和 .net webservices。一步一步作者编写代码(例如soap对象,信封,然后tranport等)。

祝你好运。

【讨论】:

  • 感谢您的快速回答。但是我想知道如何使用 ksoap2 将凭据传递给具有身份验证方法“表单”的 .net Web 服务(我使用的是 aspx 文件)
  • 通过从 logcat 添加代码 sn-ps 和异常或 wotever 使您的问题更清楚,然后这里的人将能够为您提供帮助。给的信息是不够的哥们。
  • 我希望你能更好地理解我的问题。我从上周就开始了,任何帮助都会很有用
猜你喜欢
  • 1970-01-01
  • 2011-07-26
  • 1970-01-01
  • 2011-10-12
  • 2012-04-18
  • 1970-01-01
  • 1970-01-01
  • 2012-06-19
  • 2016-02-20
相关资源
最近更新 更多