问题
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?
This is the code I'm using
{
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
回答1:
Please try following code..
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();
回答2:
I see a couple potential problems with the code.
First, and the simplest one, is that you seem to be missing a space after the word "Basic" in this line
connection.setRequestProperty("Authorization", "Basic" +
new String(getEncode()));
Change "Basic"
to "Basic "
.
Secondly, you are passing the authorization credentials to the webserver with the url. Normally, that's ok. But, another way to do it is to wait for the webserver to challenge you. If you look at this thread:
http://supportforums.blackberry.com/t5/Java-Development/HTTPS-Net-Web-Service-with-Basic-authentication/td-p/1615931
See MSohm's response:
If you are using a BlackBerry Enterprise Server (or the MDS-CS Simulator), keep this in mind:
Issues with BlackBerry MDS Connection Service when using Pre-emptive Authentication
It looks like, if your transport is BES or MDS, then this also might cause you problems. If you pick another transport (e.g. direct TCP, or Wi-Fi), then this probably isn't an issue.
Here's the reference document on how BlackBerry (RIM) suggests you make these requests
回答3:
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;
}
}
来源:https://stackoverflow.com/questions/11294892/authentication-username-password-url-in-blackberry