【问题标题】:Accessing HTTP authenticated SOAP webservice via KSOAP2 HttpTransportBasicAuth通过 KSOAP2 HttpTransportBasicAuth 访问经过 HTTP 身份验证的 SOAP Web 服务
【发布时间】:2011-10-12 15:50:01
【问题描述】:

我需要连接到需要授权的 web 服务。 我尝试使用标题:

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/}信封(位置:START_TAG @1:6 in java.io.InputStreamReader@ 44f632f8)

我了解,webservice 正在返回一些 HTML 格式的错误,但我如何才能看到它是什么?我可以制作这样的请求的 textview.settext() 或将其打印到 LogCat 吗?我怎样才能做到,为什么我会收到它?:(

然后,我尝试使用 HttpTransportBasicAuth 类 - 使其工作遇到很多问题 - 我必须将其添加到项目中,手动将其扩展从 HttpTransport 更改为 HttpTransportSE 并从 ServiceConnectionMIDP 更改为 ServiceConnectionSE 因为没有这样的ksoap2-android-assembly-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();

          }

最后,我尝试使用这个来源: Webservice Http Authentication - KSOAP2 on Android 但编译器不知道 HeaderProperty 是什么。我应该导入什么才可以? 请回答我如何才能看到 webservice 在<html> 标签中返回的确切错误消息,因为我在 LogCat 中收到了错误,如果我在尝试使其工作时做错了什么,请回答。

【问题讨论】:

    标签: android web-services ksoap2 http-authentication


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2012-03-11
      • 1970-01-01
      • 2011-07-26
      • 2011-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-28
      • 1970-01-01
      相关资源
      最近更新 更多