通过 KSOAP2 HttpTransportBasicAuth 访问 HTTP 身份验证的 SOAP Web 服务

发布于 2024-11-26 08:20:56 字数 4144 浏览 1 评论 0原文

我需要连接到网络服务,这需要授权。 我尝试使用标题:

Element usernameElement = new Element().createElement(NAMESPACE, "Username");
                       usernameElement.addChild(Node.TEXT, "user");
                       Element passwordElement = new Element().createElement(NAMESPACE,   "Password");
                        passwordElement.addChild(Node.TEXT, "pass");
                        Element header = new Element().createElement(NAMESPACE, "AuthHeader");
                        header.addChild(Node.ELEMENT, usernameElement);
                        header.addChild(Node.ELEMENT, passwordElement);

soapEnvelope.headerOut= new Element[]{header};

我收到错误: 07-25 14:03:20.922:WARN / System.err(584):org.xmlpull.v1.XmlPullParserException:预期:START_TAG {http://schemas.xmlsoap.org/soap/envelope/}信封(java.io.InputStreamReader@44f632f8 中的位置:START_TAG @1:6)

我明白,Web 服务正在返回一些 HTML 格式的错误,但我怎样才能看到它是什么?我可以对这样的请求进行 textview.settext() 或将其打印到 LogCat 吗?我怎样才能做到这一点以及为什么我收到它?:(

然后,我尝试使用 HttpTransportBasicAuth 类 - 遇到很多问题才能使其工作 - 我必须将其添加到项目中,手动将其扩展名从 HttpTransport 更改为 HttpTransportSE并从 ServiceConnectionMIDP 到 ServiceConnectionSE,因为 ksoap2-android-assemble-2.5.7 中没有这样的类,最后编译没有错误:

package com.android.testinet;

import org.ksoap2.transport.*;

import org.ksoap2.transport.HttpTransportSE;
import java.io.*;  

public class HttpTransportBasicAuth extends HttpTransportSE {
     private String username;   
        private String password;   
        /**  
         * Constructor with username and password  
         *   
         * @param url  
         *            The url address of the webservice endpoint  
         * @param username  
         *            Username for the Basic Authentication challenge RFC 2617   
         * @param password  
         *            Password for the Basic Authentication challenge RFC 2617  
         */   
        public HttpTransportBasicAuth(String url, String username, String password) {   
        super(url);   
        this.username = username;   
        this.password = password;   
    }   

    protected ServiceConnection getServiceConnection() throws IOException {   
        ServiceConnectionSE midpConnection = new ServiceConnectionSE(url);   
        addBasicAuthentication(midpConnection);   
        return midpConnection;   
    }   

    protected void addBasicAuthentication(ServiceConnection midpConnection) throws IOException {   
        if (username != null && password != null) {   
            StringBuffer buf = new StringBuffer(username);   
            buf.append(':').append(password);   
            byte[] raw = buf.toString().getBytes();   
            buf.setLength(0);   
            buf.append("Basic ");   
            org.kobjects.base64.Base64.encode(raw, 0, raw.length, buf);   
            midpConnection.setRequestProperty("Authorization", buf.toString());   
        }   
    }   

} 现在使用 HttpTransportBasicAuth.call 方法可以了,因为没有错误,但我仍然收到错误: 07-25 14:03:20.922:WARN / System.err(584):org.xmlpull.v1.XmlPullParserException:预期:START_TAG {http://schemas.xmlsoap.org/soap/envelope/}当我运行项目时,信封(位置:START_TAG @1:6 in java.io.InputStreamReader@44f632f8)。 这是我尝试连接到网络服务的代码:

  HttpTransportBasicAuth aht= new HttpTransportBasicAuth(URL, "user", "pass");
          try
          {

            aht.call(SOAP_ACTION, soapEnvelope);

            SoapPrimitive tmp_ResultString = (SoapPrimitive) soapEnvelope.getResponse();
            ResultString = tmp_ResultString.toString();
         // tv.setText(ResultString);
          }
          catch(Exception e)
          {
            e.printStackTrace();

          }

最后,我尝试使用这个源: Web 服务 Http 身份验证 - Android 上的 KSOAP2 但编译器不知道 HeaderProperty 是什么。我应该导入什么才可以? 请回答我如何才能看到 webservice 在 标记中返回的确切错误消息,因为我在 LogCat 中收到了错误,如果我在尝试时做错了什么,请回答使其发挥作用。

I need to connect to webservice, which requers authorization.
I tried to use headers:

Element usernameElement = new Element().createElement(NAMESPACE, "Username");
                       usernameElement.addChild(Node.TEXT, "user");
                       Element passwordElement = new Element().createElement(NAMESPACE,   "Password");
                        passwordElement.addChild(Node.TEXT, "pass");
                        Element header = new Element().createElement(NAMESPACE, "AuthHeader");
                        header.addChild(Node.ELEMENT, usernameElement);
                        header.addChild(Node.ELEMENT, passwordElement);

soapEnvelope.headerOut= new Element[]{header};

I receive the error:
07-25 14:03:20.922: WARN/System.err(584): org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope (position:START_TAG @1:6 in java.io.InputStreamReader@44f632f8)

I understand, webservice is returning some HTML formatted error, but how can i see what is it? Can i make textview.settext() of such a request or print it to LogCat? How can i make it and Why am i receiving it?:(

Then, i tried to use HttpTransportBasicAuth class - faced a lot of problems to make it work - i had to add it to the project, change manually its extension from HttpTransport to HttpTransportSE and from ServiceConnectionMIDP to ServiceConnectionSE because there wasn't such classes in ksoap2-android-assembly-2.5.7. At last it compiled without errors:

package com.android.testinet;

import org.ksoap2.transport.*;

import org.ksoap2.transport.HttpTransportSE;
import java.io.*;  

public class HttpTransportBasicAuth extends HttpTransportSE {
     private String username;   
        private String password;   
        /**  
         * Constructor with username and password  
         *   
         * @param url  
         *            The url address of the webservice endpoint  
         * @param username  
         *            Username for the Basic Authentication challenge RFC 2617   
         * @param password  
         *            Password for the Basic Authentication challenge RFC 2617  
         */   
        public HttpTransportBasicAuth(String url, String username, String password) {   
        super(url);   
        this.username = username;   
        this.password = password;   
    }   

    protected ServiceConnection getServiceConnection() throws IOException {   
        ServiceConnectionSE midpConnection = new ServiceConnectionSE(url);   
        addBasicAuthentication(midpConnection);   
        return midpConnection;   
    }   

    protected void addBasicAuthentication(ServiceConnection midpConnection) throws IOException {   
        if (username != null && password != null) {   
            StringBuffer buf = new StringBuffer(username);   
            buf.append(':').append(password);   
            byte[] raw = buf.toString().getBytes();   
            buf.setLength(0);   
            buf.append("Basic ");   
            org.kobjects.base64.Base64.encode(raw, 0, raw.length, buf);   
            midpConnection.setRequestProperty("Authorization", buf.toString());   
        }   
    }   

}
it is ok now with the HttpTransportBasicAuth.call method, as there is no an error, but i'm still receiving the error:
07-25 14:03:20.922: WARN/System.err(584): org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope (position:START_TAG @1:6 in java.io.InputStreamReader@44f632f8) when i run project.
Here's my code by which i try to connect to webservice:

  HttpTransportBasicAuth aht= new HttpTransportBasicAuth(URL, "user", "pass");
          try
          {

            aht.call(SOAP_ACTION, soapEnvelope);

            SoapPrimitive tmp_ResultString = (SoapPrimitive) soapEnvelope.getResponse();
            ResultString = tmp_ResultString.toString();
         // tv.setText(ResultString);
          }
          catch(Exception e)
          {
            e.printStackTrace();

          }

And finally, i tried to use this source:
Webservice Http Authentication - KSOAP2 on Android
but compiler do not know what HeaderProperty is. What should i import to be it ok?
Please answer how can i see what the exact error message webservice returns in <html> tag, because of what i'm receiving the error in LogCat and please answer if i'm doing anything wrong while trying to make it work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

似狗非友 2024-12-03 08:20:56

您需要使用 ksoap2-Android 2.5.7 (http://code.google.com/p/ksoap2-android/wiki/ProjectNews)

在那里您可以找到 HeaderProperty 类。您显示的链接至少对我有用。

You need to use ksoap2-Android 2.5.7 (http://code.google.com/p/ksoap2-android/wiki/ProjectNews)

There you can find the class HeaderProperty. The link you show works for me at least.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文