我有一个使用 .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