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

请尝试以下代码..

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

我使用以下代码(不记得在哪里找到它)作为自定义 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

我看到代码有几个潜在的问题。

首先,也是最简单的一个,您似乎在这一行中的“基本”一词之后缺少一个空格

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

更改"Basic""Basic "

其次,您使用 url 将授权凭据传递给网络服务器。通常情况下,没关系。但是,另一种方法是等待网络服务器挑战您。如果你看这个线程:

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 回答