使用适用于 Android 的 KSOAP 进行基本 HTTP 身份验证
我需要使用 Android 使用 SOAP Web 服务。
问题是,在请求特定功能之前,我需要使用基本的 http 请求对客户端进行身份验证。
您知道如何使用 KSOAP 来做到这一点吗?
到目前为止,我已经尝试使用 httpsTransportSE.call() 的重载方法,因为它建议我可以为 http 连接指定附加标头
headerPropertyList.add(new HeaderProperty("Authorization", "Basic : dXNlcjpwYXNz"));
“cdXNlcjpwYXNz”是base 64编码的字符串“user:pass”
public List call(String soapAction, SoapEnvelope envelope, List headers)
* @param headers a list of HeaderProperties to be http header properties when establishing the connection
private static final String SOAP_ACTION = "someaddress/IPortReporting/GetPortStatus";
private static final String METHOD_NAME = "methodname";
private static final String NAMESPACE = "http://ssn.someaddress/2.0/";
private static final String URL = "new.someaddress/functionName.svc";
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
Request.addProperty("MessageId", "1");
SoapSerializationEnvelope soapEnvelop = new SoapSerializationEnvelope(
11);
//soapEnvelop.headerOut = addHeaders();
soapEnvelop.dotNet = true;
soapEnvelop.setOutputSoapObject(Request);
List<HeaderProperty> headerPropertieList = new ArrayList<HeaderProperty>();
headerPropertyList.add(new HeaderProperty("Authorization", "Basic : cG9ydHdzOjEyM3F3ZUFTRA=="));
//HeaderProperty headerProperty = new HeaderProperty()
HttpsTransportSE httpsse = new HttpsTransportSE(URL, 443, "", 5000);
try {
httpsse.call(SOAP_ACTION, soapEnvelop, headerPropertyList);
//httpsse.call(SOAP_ACTION, soapEnvelop);
SoapPrimitive resultString = (SoapPrimitive) soapEnvelop
.getResponse();
tv.setText("Status: ");
} catch (Exception e) {
tv.setText("Some error," + " "
+ e.getMessage());
}
但我收到了消息“permission returned”的报告。
I need to consume a SOAP web service using Android.
The issue is that before request for a particular function I need to authenticate a client using basic http request.
Do you know how to do this using KSOAP?
Until this moment I have tried using overloaded method of httpsTransportSE.call() as it suggest that I can specify additional headers for http connection
headerPropertyList.add(new HeaderProperty("Authorization", "Basic : dXNlcjpwYXNz"));
"cdXNlcjpwYXNz" is base 64 encoded string of "user:pass"
public List call(String soapAction, SoapEnvelope envelope, List headers)
* @param headers a list of HeaderProperties to be http header properties when establishing the connection
private static final String SOAP_ACTION = "someaddress/IPortReporting/GetPortStatus";
private static final String METHOD_NAME = "methodname";
private static final String NAMESPACE = "http://ssn.someaddress/2.0/";
private static final String URL = "new.someaddress/functionName.svc";
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
Request.addProperty("MessageId", "1");
SoapSerializationEnvelope soapEnvelop = new SoapSerializationEnvelope(
11);
//soapEnvelop.headerOut = addHeaders();
soapEnvelop.dotNet = true;
soapEnvelop.setOutputSoapObject(Request);
List<HeaderProperty> headerPropertieList = new ArrayList<HeaderProperty>();
headerPropertyList.add(new HeaderProperty("Authorization", "Basic : cG9ydHdzOjEyM3F3ZUFTRA=="));
//HeaderProperty headerProperty = new HeaderProperty()
HttpsTransportSE httpsse = new HttpsTransportSE(URL, 443, "", 5000);
try {
httpsse.call(SOAP_ACTION, soapEnvelop, headerPropertyList);
//httpsse.call(SOAP_ACTION, soapEnvelop);
SoapPrimitive resultString = (SoapPrimitive) soapEnvelop
.getResponse();
tv.setText("Status: ");
} catch (Exception e) {
tv.setText("Some error," + " "
+ e.getMessage());
}
But I have message "permission denied" reported back.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
试试这个。这对我有用,从 Android 应用程序使用 .Net 服务。我使用过ksoap2-android-assemble-2.5.8-jar-with-dependency.jar
Try this. This worked for me, consuming .Net service from Android application. I have used ksoap2-android-assembly-2.5.8-jar-with-dependencies.jar
在与此相关的问题中, HeaderProperty 的第二个参数中没有冒号 - 它将是“Basic dXNlcjpwYXNz” - 也许这就是问题所在? (哪一个是正确的?)
At the linked question to this one, the second arg of the HeaderProperty does not have a colon in it - it would be "Basic dXNlcjpwYXNz" - maybe that's the problem? (Which one is correct?)
请参阅此内容。我已经实现了一个从 Android 应用程序调用本地 SOAP Web 服务(.asmx)的简单代码。
这是一个简单的 Web 服务,使用用户名和密码对用户进行身份验证。
希望有帮助
干杯
See this. I have implemented a simple code that calls a local SOAP web service(.asmx) from Android application.
It is a simple web service to authenticate user using username and password.
Hope that helps
Cheers
您很可能需要在 Android 清单文件中包含以下内容:
You most probably need to include the following in your Android manifest file:
您尝试过 HttpTranspostBasicAuth 吗?
以下是参考信息:
http://ksoap2.sourceforge.net/doc/api/org /ksoap2/transport/HttpTransportBasicAuth.html
Have you tried HttpTranspostBasicAuth?
Here is the reference information:
http://ksoap2.sourceforge.net/doc/api/org/ksoap2/transport/HttpTransportBasicAuth.html
仅限此工作 https://stackoverflow.com/a/5614243/7198554
....
这个->
SoapSerializationEnvelope 信封
命名空间这个 -> http://tempuri.org/
我的代码=>
在 VS C# 中
THIS WORK only https://stackoverflow.com/a/5614243/7198554
....
this - >
SoapSerializationEnvelope envelope
NAMESPACE this -> http://tempuri.org/
my code=>
In VS C#