java使用httpclient简单模拟登陆微信公众开放平台 | 张恒镭的博客

java使用httpclient简单模拟登陆微信公众开放平台

时间:14-03-31 栏目:java 作者:longteng 评论:0 点击: 6,790 次

 

注意:本文使用的不是微信公众平台的api,只是采用的模拟登陆的方式.

微信公众账号平台地址:https://mp.weixin.qq.com/

1 分析登陆信息,获取url

使用谷歌浏览器打开https://mp.weixin.qq.com/, 然后打开源码,或者点击右键审查元素(F12也可以).

image

 

我们能看到一个wxm2-loginform1c0c.js 估计一下应该是登陆的js,点击打开该js。

image

能够看到里面有这么一段:

t.post("/cgi-bin/login?lang=zh_CN", {
username: e.account,
pwd: t.md5(e.password.substr(0, 16)),
imgcode: c.data("isHide") ? "" : e.verify,
f: "json"
}, 

我们知道了真实的登陆地址其实是:

https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN 然后采用的是post请求。
上面还可以使用抓包工具(如fiddle)进行抓取分析真实地址。
2, 下面开始模拟登陆
我们使用的地址是https 貌似http也可以,为了使用https 我们采用取消绕过https检查的方式
代码如下:主要是重写了checkClientTrusted 方法 不进行验证。
package com.zhanghenglei.weixin;

import .io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.security.SecureRandom;
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..ConnectTimeoutException;
import org.apache.commons.httpclient.params.HttpConnectionParams;
import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
 
/**
 * 忽略数字证书
 * @author 
 */
public class MySecureProtocolSocketFactory implements
		SecureProtocolSocketFactory {
	SSLContext sslContext = null;
 
	private SSLContext createSSLContext(){
		 try{
			 SSLContext sslContext = SSLContext.getInstance("SSL");
			 sslContext.init(null, new TrustManager[]{new TrustAnyTrustManager()}, new SecureRandom());
			 return sslContext;			 
		 }catch (Exception e) {
			 throw new RuntimeException(e);
		}
	}
 
	private SSLContext getSSLContext(){
		if(this.sslContext == null)
		{
			this.sslContext = createSSLContext();
		}
		return this.sslContext;
	}
 
 
	@Override
	public Socket createSocket(Socket socket, String host, int port,
			boolean autoClose) throws IOException, UnknownHostException {
		return getSSLContext().getSocketFactory().createSocket(socket, host, port, autoClose);
	}
 
	@Override
	public Socket createSocket(String host, int port) throws IOException,
			UnknownHostException {
		return getSSLContext().getSocketFactory().createSocket(host, port);
	}
 
	@Override
	public Socket createSocket(String host, int port, InetAddress localAddress,
			int localPort) throws IOException, UnknownHostException {
		return getSSLContext().getSocketFactory().createSocket(host, port, localAddress, localPort);
	}
 
	@Override
	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{
		/**
		 * 重写验证方法,取消检测SSL
		 */
		@Override
		public void checkClientTrusted(X509Certificate[] arg0, String arg1)
				throws CertificateException {
 
		}
		@Override
		public void checkServerTrusted(X509Certificate[] arg0, String arg1)
				throws CertificateException {
 
		}
		@Override
		public X509Certificate[] getAcceptedIssuers() {
			return new X509Certificate[]{};
		}
	}
}

 

3、采用httpclient登陆,具体代码如下

 

Code

注意:Referer 信息必须加,不加是无法登陆的。

声明: 本文由( longteng )原创编译,转载请保留链接: java使用httpclient简单模拟登陆微信公众开放平台

java使用httpclient简单模拟登陆微信公众开放平台:等您坐沙发呢!

发表评论




------====== 本站公告 ======------
欢迎关注我的博客。

其他