[英]Android - Ksoap2 Authentication in .Net Web Service
我有一个使用.Net web 服务的 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
I was using ksoap2-Android 2.5.2 so i couldn't use the class HeaderProperty, i change to ksoap2-Android 2.5.7 and follow this link: Webservice Http Authentication - KSOAP2 on Android That works for me.
这是一个精彩的教程,向您展示如何一起使用 Ksoap 和 .net webservices。 一步一步作者编写代码(如 soap object,信封,然后传输等)。
祝你好运。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.