I want to be able to log into HTTPS websites(like gmail) with my username/password using java code.
I've tried using both my own code and the HTTPClient package(both allowed me to log into normal websites( like facebook)), but not into HTTPS websites.
Can someone tell me what is necessary to use java to sign into a https website?
Thanks!
Example Code:
import org.apache.http.*;
import org.apache.http.auth.*;
import org.apache.http.client.methods.*;
import org.apache.http.impl.client.*;
import org.apache.http.util.*;
public class ClientAuthentication {
public static void main(String[] args) throws Exception {
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
httpclient.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials("email@gmail.com", "password"));
HttpGet httpget = new HttpGet("https://www.google.com/accounts/ServiceLogin?service=mail&passive=true&rm=false&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F%3Fui%3Dhtml%26zy%3Dl&bsv=llya694le36z&ss=1&scc=1<mpl=default<mplcache=2&hl=en&from=logout");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
entity.writeTo(System.out);
}
EntityUtils.consume(entity);
} finally {
httpclient.getConnectionManager().shutdown();
}
}
}
解决方案
You are talking about authentication or connecting to https server (without client auth)?
Many sites (such as google, facebook) uses OAuth or OpenID - read about it.
If its sth else (like you are using simply POST). What is the problem? Are you getting stacktrace?