0

ユーザー名とパスワードが必要なサイトへの接続をセットアップしていますが、接続できません。理由がわかりません。

これは私が使用しているコードです

 {
    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;
}

実際のパスワードは「@@」で始まり、大文字があります

4

3 に答える 3

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

}
于 2012-07-03T06:28:17.893 に答える
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();
于 2012-07-02T14:35:00.500 に答える
0

コードに潜在的な問題がいくつか見られます。

まず、最も単純なのは、この行の「基本」という単語の後にスペースがないように見えることです

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

に変更"Basic""Basic "ます。

次に、URL を使用して認証資格情報を Web サーバーに渡します。通常、それで問題ありません。しかし、それを行う別の方法は、Web サーバーが挑戦するのを待つことです。このスレッドを見ると:

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

MSohm の応答を参照してください。

BlackBerry Enterprise Server (または MDS-CS Simulator) を使用している場合は、次のことに注意してください。

プリエンプティブ認証を使用する場合の BlackBerry MDS Connection Service の問題

トランスポートが BES または MDS の場合、これも問題を引き起こす可能性があるようです。別のトランスポート (ダイレクト TCP や Wi-Fi など) を選択した場合、これはおそらく問題になりません。

以下は、BlackBerry (RIM)がこれらの要求を行うことを提案する方法に関する参照ドキュメントです。

于 2012-07-03T00:35:21.563 に答える