package sota;import java.io.IOException;import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.protocol.Protocol;public class Picasa {
public Picasa() throws HttpException, IOException
{
Reuqest();
}
public void Reuqest() throws HttpException, IOException
{
Protocol myhttps = new Protocol("https",new MySecureProtocolSocketFactory(), 443);
Protocol.registerProtocol("https", myhttps);
HttpClient httpClient = new HttpClient();
System.setProperty("apache.commons.httpclient.cookiespec", "compatibility");
httpClient.getHostConfiguration().setHost("www.google.com", 443, "https");
String actionUrl = "https://www.google.com/accounts/ServiceLoginAuth?service=lh2";
PostMethod postmethod = new PostMethod(actionUrl);
NameValuePair Email = new NameValuePair("Email", "[email protected]");
NameValuePair password = new NameValuePair("password" , "123456" );
NameValuePair ltmpl = new NameValuePair("ltmpl" , "gp" ); NameValuePair cont = new NameValuePair("continue" , "http://picasaweb.google.com/lh/login?continue=http%3A%2F%2Fpicasaweb.google.com%2Fhome" );
NameValuePair service = new NameValuePair("service" , "lh2" ); NameValuePair hl = new NameValuePair("hl" , "zh_CN" );
NameValuePair PersistentCookie = new NameValuePair("PersistentCookie" , "yes" );
NameValuePair rmShown = new NameValuePair("rmShown" , "1" );
NameValuePair[] data = {Email,password,cont,ltmpl,service,hl,PersistentCookie,rmShown};
postmethod.setRequestBody(data);
postmethod.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=gb2312");
httpClient.executeMethod(postmethod);
Cookie[] cookies=httpClient.getState().getCookies();
for(int i=0;i<cookies.length;i++)
{
System.out.print(cookies[i].toString());
}
httpClient.getState().addCookies(cookies);
postmethod.releaseConnection();
int state = postmethod.getStatusCode();
if ((state == HttpStatus.SC_MOVED_TEMPORARILY) ||
(state == HttpStatus.SC_MOVED_PERMANENTLY) ||
(state == HttpStatus.SC_SEE_OTHER) ||
(state == HttpStatus.SC_TEMPORARY_REDIRECT))
{
Header header = postmethod.getResponseHeader("location");
if(header != null)
{
String newUrl = header.getValue();
GetMethod getMethod = new GetMethod(newUrl);
getMethod.setDoAuthentication(true);
httpClient.executeMethod(getMethod);
System.out.println(getMethod.getResponseBodyAsString());
} }
else
{
System.out.println("登陆失败!");
}
}
}
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.protocol.Protocol;public class Picasa {
public Picasa() throws HttpException, IOException
{
Reuqest();
}
public void Reuqest() throws HttpException, IOException
{
Protocol myhttps = new Protocol("https",new MySecureProtocolSocketFactory(), 443);
Protocol.registerProtocol("https", myhttps);
HttpClient httpClient = new HttpClient();
System.setProperty("apache.commons.httpclient.cookiespec", "compatibility");
httpClient.getHostConfiguration().setHost("www.google.com", 443, "https");
String actionUrl = "https://www.google.com/accounts/ServiceLoginAuth?service=lh2";
PostMethod postmethod = new PostMethod(actionUrl);
NameValuePair Email = new NameValuePair("Email", "[email protected]");
NameValuePair password = new NameValuePair("password" , "123456" );
NameValuePair ltmpl = new NameValuePair("ltmpl" , "gp" ); NameValuePair cont = new NameValuePair("continue" , "http://picasaweb.google.com/lh/login?continue=http%3A%2F%2Fpicasaweb.google.com%2Fhome" );
NameValuePair service = new NameValuePair("service" , "lh2" ); NameValuePair hl = new NameValuePair("hl" , "zh_CN" );
NameValuePair PersistentCookie = new NameValuePair("PersistentCookie" , "yes" );
NameValuePair rmShown = new NameValuePair("rmShown" , "1" );
NameValuePair[] data = {Email,password,cont,ltmpl,service,hl,PersistentCookie,rmShown};
postmethod.setRequestBody(data);
postmethod.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=gb2312");
httpClient.executeMethod(postmethod);
Cookie[] cookies=httpClient.getState().getCookies();
for(int i=0;i<cookies.length;i++)
{
System.out.print(cookies[i].toString());
}
httpClient.getState().addCookies(cookies);
postmethod.releaseConnection();
int state = postmethod.getStatusCode();
if ((state == HttpStatus.SC_MOVED_TEMPORARILY) ||
(state == HttpStatus.SC_MOVED_PERMANENTLY) ||
(state == HttpStatus.SC_SEE_OTHER) ||
(state == HttpStatus.SC_TEMPORARY_REDIRECT))
{
Header header = postmethod.getResponseHeader("location");
if(header != null)
{
String newUrl = header.getValue();
GetMethod getMethod = new GetMethod(newUrl);
getMethod.setDoAuthentication(true);
httpClient.executeMethod(getMethod);
System.out.println(getMethod.getResponseBodyAsString());
} }
else
{
System.out.println("登陆失败!");
}
}
}
解决方案 »
- 父类与子类间,方法名相同参数不同的方法,能算是多态表现形式吗?能算是重载吗?
- 做了个评分的小程序,结果怎么数组比较结果全是0??
- 容器的最小大小如何设置?
- java中关于InputStream流的读取问题,available()返回0,用read()还能读,为什么?
- 第一次发这样的贴,希望对大家会有一点点的帮助
- 求《java数据库应用程序编程指南》电子版
- 这种错误如何解决?
- JDialog的哪个方法是设置对话框右上角的"×"按钮无效?
- 请帮帮我
- 送分:各位兄台,小弟要去上学了,不知以后还能不能来,就把我的2121分全送了....
- 关于自定义JTabbedPane的自定义TabComponet的问题
- int 的初始值问题!
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;import javax.net.SocketFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;import org.apache.commons.httpclient.ConnectTimeoutException;
import org.apache.commons.httpclient.params.HttpConnectionParams;
import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;public class MySecureProtocolSocketFactory implements SecureProtocolSocketFactory {
static{
System.out.println(">>>>in MySecureProtocolSocketFactory>>");
}
private SSLContext sslcontext = null;
private SSLContext createSSLContext() {
SSLContext sslcontext=null;
try {
sslcontext = SSLContext.getInstance("SSL");
sslcontext.init(null, new TrustManager[]{new TrustAnyTrustManager()}, new java.security.SecureRandom());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
return sslcontext;
}
private SSLContext getSSLContext() {
if (this.sslcontext == null) {
this.sslcontext = createSSLContext();
}
return this.sslcontext;
}
public Socket createSocket(Socket socket, String host, int port, boolean autoClose)
throws IOException, UnknownHostException {
return getSSLContext().getSocketFactory().createSocket(
socket,
host,
port,
autoClose
);
} public Socket createSocket(String host, int port) throws IOException,
UnknownHostException {
return getSSLContext().getSocketFactory().createSocket(
host,
port
);
}
public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort)
throws IOException, UnknownHostException {
return getSSLContext().getSocketFactory().createSocket(host, port, clientHost, clientPort);
} public Socket createSocket(String host, int port, InetAddress localAddress,
int localPort, HttpConnectionParams params) throws IOException,
UnknownHostException, ConnectTimeoutException {
if (params == null) {
throw new IllegalArgumentException("Parameters may not be null");
}
int timeout = params.getConnectionTimeout();
SocketFactory socketfactory = getSSLContext().getSocketFactory();
if (timeout == 0) {
return socketfactory.createSocket(host, port, localAddress, localPort);
} else {
Socket socket = socketfactory.createSocket();
SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
SocketAddress remoteaddr = new InetSocketAddress(host, port);
socket.bind(localaddr);
socket.connect(remoteaddr, timeout);
return socket;
}
}
//自定义私有类
private static class TrustAnyTrustManager implements X509TrustManager {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[]{};
}
}
}
>>>>in MySecureProtocolSocketFactory>>
GoogleAccountsLocale_session=zh_CN登陆失败!