2011-03-03 75 views
5

我需要使用Android來使用SOAP Web服務。使用KSOAP進行Android的基本HTTP認證

問題是,在請求特定函數之前,我需要使用基本http請求來驗證客戶端。

你知道如何使用KSOAP來做到這一點嗎?

直到這一刻我使用httpsTransportSE.call的重載方法(),因爲它表明,我可以爲HTTP連接指定額外的頭已經嘗試

(參考: https://github.com/mosabua/ksoap2-android/blob/master/ksoap2-j2se/src/main/java/org/ksoap2/transport/HttpTransportSE.java

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()); 
     } 

但我有消息「權限被拒絕」報告回來。

+0

當實例headerPropertyList你寫屬性,但後來,當你設置它,你怎麼稱呼它propertie可能? – GriffiN 2011-07-12 20:26:35

回答

2

你很可能需要在您的Android清單文件中的以下內容:

<uses-permission android:name="android.permission.INTERNET" /> 
+1

<使用權限android:name =「android。permission.INTERNET「/> – Neem 2011-03-20 17:06:29

4

在鏈接的問題這其中,HeaderProperty的第二ARG沒有在這一個冒號 - 這將是「基本dXNlcjpwYXNz」 - 也許這就是問題所在? (哪一個是正確的?)

+0

我確認了 - 絕對沒有冒號應該在」Basic ...「字符串中。 – Conrad 2011-06-07 18:43:28

10

試試這個。這爲我工作,從android應用程序使用.Net服務。我已經使用kso​​ap2-機器人組裝-2.5.8-JAR-與-dependencies.jar

String NAMESPACE = "http://www.namespace.com/"; 
String METHOD_NAME = "MethodName"; 
String SOAP_ACTION = "http://www.namespace.com/MethodName"; 
String URL = "https://www.namespace.com/services/Service.asmx"; 

     SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
      // Set all input params 
      request.addProperty("property", "value"); 
      SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
     // Enable the below property if consuming .Net service 
    envelope.dotNet = true; 
    envelope.setOutputSoapObject(request); 

    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
    try 
    { 
     List<HeaderProperty> headerList = new ArrayList<HeaderProperty>(); 
     headerList.add(new HeaderProperty("Authorization", "Basic " + org.kobjects.base64.Base64.encode("username:password".getBytes()))); 

     androidHttpTransport.call(SOAP_ACTION, envelope, headerList); 
     SoapObject response = (SoapObject)envelope.getResponse(); 
     //response.getProperty(0).toString(); 
    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 
+0

如何在上例中添加用戶名和密碼 – 2014-05-02 11:40:33

+0

得到格式錯誤的輸入:混合內容 – 2014-10-31 11:41:03

2

this。我實現了一個簡單的代碼,用於從Android應用程序調用本地SOAP Web服務(.asmx)。

這是一個簡單的網絡服務,使用用戶名和密碼對用戶進行身份驗證。

希望幫助

乾杯

0

本作品僅https://stackoverflow.com/a/5614243/7198554 ....

soapEnvelope.headerOut = new Element[1]; 

本 - >SoapSerializationEnvelope envelope 命名空間此 - >http://tempuri.org/ 我的代碼=>

public Element buildAuthHeader() { 
Element h = new Element().createElement(NAMESPACE, "AuthHeader"); 
Element username = new Element().createElement(NAMESPACE, "Username"); 
username.addChild(Node.TEXT, "test"); 
h.addChild(Node.ELEMENT, username); 
// Element pass = new Element().createElement(NAMESPACE, "pass"); 
// pass.addChild(Node.TEXT, pass); 
// h.addChild(Node.ELEMENT, pass); 
return h; 

在VS C#

public class AuthHeader : SoapHeader 
{ 
public string Username; 
//public string Password; 
} 
相關問題