Android - .Net Web 服务中的 Ksoap2 身份验证
我有一个 Android 应用程序,它使用 .Net Web 服务,我正在使用 ksoap2。 我想传递凭据(用户名和密码),因此我需要使用一种身份验证类型(表单、基本等),但我不知道如何。
我从以下位置下载了一个安全的 WebService(此示例要求提供凭据): Web 服务安全性
[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
I have an android app that consumes a .Net web service for that i'm using ksoap2. I want to pass credentials (username and password) so I' need to use a type of authentication (form, Basic, ...), but i don't know how.
I had download a secure WebService (this sample ask for credentials) from:
Web Services Security
[WebMethod]
public string HelloWorld()
{
return "Hello " + Context.User.Identity.Name;
}
I call the webMethod with basic code and that's why i get an error, somewhere i need to pass username and password but i don't know how. There are links related, that use NTCredentials, envelope.headerOut and HeaderProperty (Class that i don't have), i try all and nothing.
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());
}
}
The exception occurs in httpTransportSE.call(soapAction, envelope), because as i say i'm not passing the username and password
org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/envelope/}Envelope (position:START_TAG <html>@1:6 in java.io.InputStreamReader@44f13428)
In ht.responseDump I get in html Code:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个精彩的教程向您展示如何使用 Ksoap和 .net web 服务在一起。作者一步步编写代码(例如肥皂对象、信封、然后传输等)。
祝你好运。
This is a wonderful tutorial shows you how to use Ksoap and .net webservices together. step by step author writes the code (such as soap object, envelop, then tranport etc etc).
Good luck.