在黑莓验证用户名密码网址 [英] authentication username password url in blackberry
本文介绍了在黑莓验证用户名密码网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我设置到需要用户名和密码,一个站点的连接,但它不连接,我不知道原因你能帮助我吗?
I'm setting up a connection to a site which requires a username and password but it doesn't connect and I don't know the reason can you help me with this please?
这是code我用
{
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;
}
实际的密码并用@@开始,有一个大写字母
The actual password does start with "@@" and there's a capital letter
推荐答案
我用下面的code(不记得在那里我发现它)为起点,自定义NTLM连接器实现。它工作得很好。希望它可以帮助你。搜索结果
I used the following code (don't remember where I found it) as starting point for a custom Ntlm Connector implementation. It worked fine. Hope it may help you.
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;
}
}
这篇关于在黑莓验证用户名密码网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文