我正在建立一个需要用户名和密码的网站的连接,但它没有连接,我不知道你能帮我解决这个问题的原因吗?
这是我正在使用的代码
{
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 :(得分: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();
答案 1 :(得分:0)
我发现代码存在一些潜在的问题。
首先,也是最简单的一个,就是你似乎在这行中的“基本”一词之后缺少一个空格
connection.setRequestProperty("Authorization", "Basic" +
new String(getEncode()));
将"Basic"
更改为"Basic "
。
其次,您将授权凭据传递给具有URL的Web服务器。通常,没关系。但是,另一种方法是等待网络服务器挑战你。如果你看一下这个帖子:
见MSohm的回复:
如果您使用的是BlackBerry Enterprise Server(或MDS-CS) 模拟器),记住这一点:
使用Pre-emptive时BlackBerry MDS Connection Service的问题 认证
看起来,如果你的运输是BES或MDS,那么这也可能会给你带来麻烦。如果你选择另一个传输(例如直接TCP或Wi-Fi),那么这可能不是问题。
以下是关于BlackBerry(RIM)suggests you make these requests
的参考文档答案 2 :(得分:0)
我使用以下代码(不记得我在哪里找到它)作为自定义Ntlm Connector实现的起点。它工作正常。希望它可以帮助你。
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;
}
}