`
ak23173969
  • 浏览: 29371 次
社区版块
存档分类
最新评论

模拟登入微信公众平台

阅读更多

主要的目的是通过后台模拟登入微信公众平台修改url和token,用到了httpclient框架具体看代码

 

package com.kzkj.httpclient.Test;

import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.junit.Test;

public class Test3 {
	public HttpClient client = new DefaultHttpClient();
	
	@Test
	public void test() throws Exception{
		String username="***********";
		String password="*********";
		String oppenid1="*********";
		String oppenidw="*********";
		String url="http://www.****.com/WeiXingTest/coreServlet";
		String robackToken="woaimeiz";
		String token="";
		String checkCode=getCheckCode(username);
		password=MD5Encode(password);
		String content=getLogins(username,password,checkCode);
		token=getToken(content);
		//editAssistantOpenID(oppenid1,token);
		editCommonInteface(token,url,robackToken);
	}
	/**
	 * 修改公众号接口配置信息
	 * @param url,robackToken,token
	 * */
	public void editCommonInteface(String token,String application_url,String robackToken)throws Exception{
		
		String PostUrl="https://mp.weixin.qq.com/cgi-bin/callbackprofile?t=ajax-response&token="+token+"&lang=zh_CN";
		
		String Header="https://mp.weixin.qq.com/cgi-bin/advanced?action=interface&t=advanced/interface&token="+token+"&lang=zh_CN";
		
		HttpPost cfgPost = new HttpPost(PostUrl);
		
		cfgPost.addHeader("Referer", Header);

		List<NameValuePair> nvp_2 = new ArrayList<NameValuePair>();
		
		nvp_2.add(new BasicNameValuePair("callback_token", robackToken));
		nvp_2.add(new BasicNameValuePair("url", application_url));
		cfgPost.setEntity(new UrlEncodedFormEntity(nvp_2));

		System.out.println(EntityUtils.toString(client.execute(cfgPost).getEntity()));
		cfgPost.releaseConnection();
	}
	
	
	/**
	 * 修改公众号手机助手的绑定微信号
	 * @param openID,token
	 * */
	private void editAssistantOpenID(String openID,String token)throws Exception{
		
		String url="https://mp.weixin.qq.com/cgi-bin/binduser?cgi=binduser&t=ajax-response&binduser="+openID;
		
		HttpPost post3 = new HttpPost(url);
		
		post3.addHeader("Referer", "https://mp.weixin.qq.com/");
		
		List<NameValuePair> nvp3 = new ArrayList<NameValuePair>();
		nvp3.add(new BasicNameValuePair("ajax", "1"));
		nvp3.add(new BasicNameValuePair("token", token));
		post3.setEntity(new UrlEncodedFormEntity(nvp3));
		HttpResponse resp3 = client.execute(post3);
		//System.out.println(EntityUtils.toString(resp3.getEntity()));
		post3.releaseConnection();
	}
	
	
	
	/**
	 * 获取Token
	 * @param reposne返回的数据
	 * @return Token
	 * */
	private String getToken(String response){
		String [] bs=response.split("&token=")[1].split("','");	
		String token=bs[0].split(",")[0];
		token=token.substring(0, token.length()-1);
		return token;
	}
	
	
	/**MD5加密
	 * @param 加密前密码
	 * @return 加密后密码
	 * */
	private String MD5Encode(String password) {
		MessageDigest md5 = null;
		byte[] bs = password.getBytes();
		StringBuffer buffer = new StringBuffer();
		try {
			md5 = MessageDigest.getInstance("MD5");
			md5.update(bs);
			bs = md5.digest();
			for (int i = 0; i < bs.length; i++) {

				if ((bs[i] & 0xff) < 0x10) {
					buffer.append("0");
				}
				buffer.append(Long.toString(bs[i] & 0xff, 16));
			}
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return buffer.toString();
	}
	/**
	 * 获取验证码
	 * @param 用户名称
	 * @return 验证码
	 * */ 
	private  String getCheckCode(String username)throws Exception{
		
		String url="https://mp.weixin.qq.com/cgi-bin/verifycode?username="+username+"&r="+System.currentTimeMillis();
		HttpGet get_1 = new HttpGet(url);
		HttpResponse resp_1 = client.execute(get_1);
		BufferedImage image =  ImageIO.read(resp_1.getEntity().getContent());
		String checkCode = JOptionPane.showInputDialog(new ImageIcon(image));
		return checkCode;
	}
	// 请求登录
	private void getLogin()throws Exception{
		// 获取验证码
		HttpGet get_1 = new HttpGet("https://mp.weixin.qq.com/cgi-bin/verifycode?username=ejcall@163.com&r="+System.currentTimeMillis());
		HttpResponse resp_1 = client.execute(get_1);
		BufferedImage image =  ImageIO.read(resp_1.getEntity().getContent());
		String checkCode = JOptionPane.showInputDialog(new ImageIcon(image));
		System.out.println(checkCode);
		get_1.releaseConnection();

		// 请求登录
		HttpPost post = new HttpPost("https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN");
		post.addHeader("Referer", "https://mp.weixin.qq.com/");

		List<NameValuePair> nvp = new ArrayList<NameValuePair>();
		nvp.add(new BasicNameValuePair("f", "json"));
		nvp.add(new BasicNameValuePair("imgcode", checkCode));
		nvp.add(new BasicNameValuePair("pwd", "1d856dafc0930157041e8c7c4bb11ed3"));
		nvp.add(new BasicNameValuePair("username", "ejcall@163.com"));
		post.setEntity(new UrlEncodedFormEntity(nvp));
		System.out.println(EntityUtils.toString(client.execute(post).getEntity()));
		post.releaseConnection();


		// 修改配置
		HttpPost cfgPost = new HttpPost("https://mp.weixin.qq.com/cgi-bin/callbackprofile?t=ajax-response&token=288164033&lang=zh_CN");
		cfgPost.addHeader("Referer", "https://mp.weixin.qq.com/cgi-bin/advanced?action=interface&t=advanced/interface&token=288164033=&lang=zh_CN");

		List<NameValuePair> nvp_2 = new ArrayList<NameValuePair>();
		nvp_2.add(new BasicNameValuePair("callback_token", "08712013191636045726"));
		nvp_2.add(new BasicNameValuePair("url", "http://0992.mpb.weduty.com/mp/B1334055059"));
		cfgPost.setEntity(new UrlEncodedFormEntity(nvp_2));

		System.out.println(EntityUtils.toString(client.execute(cfgPost).getEntity()));
		cfgPost.releaseConnection();
	}
	/**
	 * 请求登入
	 * @param 用户名,加密后密码,验证码
	 * @return  reposne返回的数据,包含Token的String字段
	 * */
	private String getLogins(String user,String pas,String checkCode)throws Exception{
		String resultCont="";
		// 请求登录
		HttpPost post = new HttpPost("https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN");
		post.addHeader("Referer", "https://mp.weixin.qq.com/");

		List<NameValuePair> nvp = new ArrayList<NameValuePair>();
		nvp.add(new BasicNameValuePair("f", "json"));
		nvp.add(new BasicNameValuePair("imgcode", checkCode));
		nvp.add(new BasicNameValuePair("pwd", pas));
		nvp.add(new BasicNameValuePair("username", user));
		post.setEntity(new UrlEncodedFormEntity(nvp));
		resultCont=EntityUtils.toString(client.execute(post).getEntity());
		post.releaseConnection();
		return resultCont;
	}

}

 

 

 

分享到:
评论
7 楼 ak23173969 2015-11-20  
我很久没有碰过微信了
6 楼 Angelet 2015-08-31  
GodIT 写道
String oppenid1="*********"; 
String oppenidw="*********";  是什么东西?





我每次调用返回的是:{"ret":"-302", "msg":"verify token fail"}




请问你的问题解决了么?这个返回到底是哪里出问题了啊?
5 楼 a3229526 2014-04-21  
修改url和token的接口已更新为:
String PostUrl= "https://mp.weixin.qq.com/advanced/callbackprofile?t=ajax-response&token="+token+"&lang=zh_CN";

String Header="https://mp.weixin.qq.com/advanced/advanced?action=interface&t=advanced/interface&token="+token+"&lang=zh_CN";
4 楼 pixar 2014-04-01  
wiseyl 写道
楼主你好:最近发现    // 修改配置 
171.        HttpPost cfgPost = new HttpPost("https://mp.weixin.qq.com/cgi-bin/callbackprofile?t=ajax-response&token=288164033&lang=zh_CN"); 
172.        cfgPost.addHeader("Referer", "https://mp.weixin.qq.com/cgi-bin/advanced?action=interface&t=advanced/interface&token=288164033=&lang=zh_CN");

之前可以的。 现在不行了,我怀疑是微信后台发生改变。 请问上面的URL是如何获取的,请指教,谢谢........

同问
3 楼 wiseyl 2014-03-18  
楼主你好:最近发现    // 修改配置 
171.        HttpPost cfgPost = new HttpPost("https://mp.weixin.qq.com/cgi-bin/callbackprofile?t=ajax-response&token=288164033&lang=zh_CN"); 
172.        cfgPost.addHeader("Referer", "https://mp.weixin.qq.com/cgi-bin/advanced?action=interface&t=advanced/interface&token=288164033=&lang=zh_CN");

之前可以的。 现在不行了,我怀疑是微信后台发生改变。 请问上面的URL是如何获取的,请指教,谢谢........
2 楼 GodIT 2014-01-17  
String oppenid1="*********"; 
String oppenidw="*********";  是什么东西?





我每次调用返回的是:{"ret":"-302", "msg":"verify token fail"}



1 楼 GodIT 2014-01-17  

测试的时候报错误:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path validation failed: java.security.cert.CertPathValidatorException: basic constraints check failed: pathLenConstraint violated - this cert must be the last cert in the certification path
at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:174)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1591)
at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:187)
at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:181)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:975)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:123)
at com.sun.net.ssl.internal.ssl.Handshaker.processLoop(Handshaker.java:516)
at com.sun.net.ssl.internal.ssl.Handshaker.process_record(Handshaker.java:454)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:884)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1096)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1123)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1107)
at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:533)
at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:401)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:178)
at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:304)
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:610)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:445)
at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:863)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
at weixin.autologin.test.AutoLogin.getCheckCode(AutoLogin.java:142)
at weixin.autologin.test.AutoLogin.test(AutoLogin.java:41)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: sun.security.validator.ValidatorException: PKIX path validation failed: java.security.cert.CertPathValidatorException: basic constraints check failed: pathLenConstraint violated - this cert must be the last cert in the certification path
at sun.security.validator.PKIXValidator.doValidate(PKIXValidator.java:251)
at sun.security.validator.PKIXValidator.doValidate(PKIXValidator.java:234)
at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:158)
at sun.security.validator.Validator.validate(Validator.java:218)
at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:126)
at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:209)
at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:249)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:954)
... 42 more
Caused by: java.security.cert.CertPathValidatorException: basic constraints check failed: pathLenConstraint violated - this cert must be the last cert in the certification path
at sun.security.provider.certpath.PKIXMasterCertPathValidator.validate(PKIXMasterCertPathValidator.java:139)
at sun.security.provider.certpath.PKIXCertPathValidator.doValidate(PKIXCertPathValidator.java:316)
at sun.security.provider.certpath.PKIXCertPathValidator.engineValidate(PKIXCertPathValidator.java:178)
at java.security.cert.CertPathValidator.validate(CertPathValidator.java:250)
at sun.security.validator.PKIXValidator.doValidate(PKIXValidator.java:246)
... 49 more

相关推荐

Global site tag (gtag.js) - Google Analytics