2012-07-02 48 views
0

我建立了一個網站的連接,需要用戶名和密碼,但它沒有連接,我不知道你可以幫我這個理由嗎?身份驗證用戶名密碼URL在黑莓

這是我使用

{ 
    HttpsConnection connection; 
    connection = (HttpsConnection) Connector.open(url + 
     getBlackBerryConnectionParams()); 

    connection.setRequestProperty("Authorization", "Basic" + 
     new String(getEncode())); 
}     

public byte[] getEncode() { 
    String login = "user:@@iPass"; 
    byte[] encoded = null; 
    try { 
     encoded = Base64OutputStream.encode(login.getBytes(), 0, 
       login.length(), false, false); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return encoded; 
} 

的真正密碼並啓動以「@@」,並有一個大寫字母的代碼

+0

「基本」後面缺少空格。但是,可能還有其他問題......請參閱下面的答案。 – Nate

回答

0

請嘗試以下代碼..

String URL = "URL"+methodName+parameterString+"deviceside=true"; 
connection = (HttpConnection) Connector.open(URL); 
String authenticationParameter = "username"+":"+"password"; 
byte[] encoded = Base64OutputStream.encode(authenticationParameter.getBytes(), 0, authenticationParameter.length(), false, false); 
connection.setRequestProperty("Authorization", "Basic "+ new String(encoded)); 
rc = connection.getResponseCode(); 
+0

其實這和我上面寫的是一樣的! – mhGaber

0

我看到幾個潛在代碼的問題。

首先,也是最簡單的一種,就是你似乎在這條線

connection.setRequestProperty("Authorization", "Basic" + 
    new String(getEncode())); 

變化"Basic"失蹤單詞「基本」後的空間"Basic "

其次,您將授權憑證傳遞給帶有url的網絡服務器。通常情況下,沒關係。但是,另一種方法是等待Web服務器向您提出挑戰。如果你看看這個線程:

http://supportforums.blackberry.com/t5/Java-Development/HTTPS-Net-Web-Service-with-Basic-authentication/td-p/1615931

見MSohm的迴應:

如果您使用的是黑莓企業服務器(或MDS-CS 模擬器),記住這一點:

使用先發制人 認證時

問題黑莓MDS連接服務

看起來,如果您的交通工具是BES或MDS,那麼這也可能會導致您遇到問題。如果您選擇其他傳輸方式(例如直接TCP或Wi-Fi),那麼這可能不是問題。

這裏的黑莓(RIM)如何suggests you make these requests

0

我用下面的代碼(不記得在那裏我發現它)作爲起點,自定義NTLM連接器實現的參考文件。它運行良好。希望它可以幫助你。

package net; 

import java.io.IOException; 

import javax.microedition.io.Connector; 
import javax.microedition.io.HttpConnection; 
import javax.microedition.io.StreamConnection; 

import net.rim.device.api.io.Base64OutputStream; 

public class NtlmConnector implements IConnector { 

    private String url; 
    private String domain; 
    private String username; 
    private String password; 

    public NtlmConnector(String url, String domain, String username, String password) { 

     this.url = url; 
     this.domain = domain; 
     this.username = username; 
     this.password = password; 
    } 

    private HttpConnection openAuthenticatedConnection() throws IOException { 

     StreamConnection netStream = (StreamConnection)Connector.open(url); 
     HttpConnection httpConnection = (HttpConnection)netStream; 
     String credentials = domain + "\\" + username + ":" + password;   
     byte[] encodedCredentials = Base64OutputStream.encode(credentials.getBytes(), 0,  credentials.length(), false, false); 
     httpConnection.setRequestProperty("Authorization", "Basic " + new String(encodedCredentials)); 

     return httpConnection; 
    } 

    private void mapResultToConnectionStatus(ConnectorResult result, int connectionStatus) { 

     switch (connectionStatus) { 
     case (HttpConnection.HTTP_OK): 
      result.setConnectionDone(true); 
      break; 

     case (HttpConnection.HTTP_UNAUTHORIZED): 
      result.setConnectionDone(false); 
      //TODO: add detailed error 
      break; 

     default: 
      result.setConnectionDone(false); 
      break; 
     } 
    } 

    public ConnectorResult connect() { 

     ConnectorResult result = new ConnectorResult(); 

     try 
     { 
      HttpConnection connection = openAuthenticatedConnection(); 

      int responseStatus = connection.getResponseCode(); 

      connection.close(); 

      mapResultToConnectionStatus(result, responseStatus); 

     } 
     catch (IOException e) 
     { 
      //TODO: map into result error detail    
     }  

     return result; 
    } 

} 
相關問題