大橙子网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

java伪代码登录 java命令模式伪代码

java 模拟post登录

这个要分两步,先用GET方法取得页面源码,分析出mpc的值,然后用POST方法发送数据就能登录了。当然一切工作之前要设置好CookieHandler

成都创新互联公司专注于金昌企业网站建设,自适应网站建设,商城网站开发。金昌网站建设公司,为金昌等地区提供建站服务。全流程定制网站开发,专业设计,全程项目跟踪,成都创新互联公司专业和态度为您提供的服务

post页面:

;pageref=

post参数就4个启旅悔而已,

name=yourNamepwd=yourPasswordB12=Loginmpc=分析得到的mpc

看镇余如下例子:

将这个例子中的如下语句改一下就能收到数据的

connection.getInputStream().close();

//

import java.io.BufferedInputStream;

import java.io.DataOutputStream;

import java.io.InputStream;

import java点虐 .CookieHandler;

import java点虐 .CookieManager;

import java点虐 .CookiePolicy;

import java点虐 .HttpURLConnection;

import java点虐 .URL;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class SongTaste {

private static String baseURL="";

private static String loginURL=baseURL+"/signin.php";

private static String actionURL=baseURL+"悄正/info_oper.php?tag=signinpageref=";

private static String musicURL=baseURL+"/music/";

private static CookieManager cm;

static{

cm=new CookieManager();

cm.setCookiePolicy(CookiePolicy.ACCEPT_ALL);

CookieHandler.setDefault(cm);

}

public static void main(String[] args) throws Exception{

// HttpURLConnection.setFollowRedirects(true);

login("abcmusic","000000");

listContents();

}

private static void listContents() throws Exception {

byte[] dat=new HttpGet(musicURL).data;

String src=new String(dat,"gbk");

Matcher m=Pattern点抗 pile("MSL\\(.*?\\)").matcher(src);

while(m.find())

System.out.println(m.group());

}

private static void login(String name,String pwd){

byte[] dat=new HttpGet(loginURL).data;

String src=new String(dat);

Matcher m=Pattern点抗 pile("name=mpc.*?").matcher(src);

String mpc="";

if(m.find()){

mpc=m.group();

// System.out.println(mpc);

mpc=mpc.substring(15,mpc.length()-1);

// System.out.println(mpc);

}

//do login

new HttpPost(actionURL,String.format("name=%spwd=%sB12=Loginmpc=%s",name,pwd,mpc));

}

private static class HttpGet extends Thread{

private static final int bufferSize=1024;

private String ustr;

private byte[] data;

private HttpGet(String u,String...ref){

ustr=u;

start();

try {join();} catch (Exception e) {}

}

public void run(){

try{

URL u = new URL(ustr);

HttpURLConnection uc=(HttpURLConnection)u.openConnection();

byte[] b={};

byte[] t=new byte[bufferSize];

int r;

BufferedInputStream bin=new BufferedInputStream(uc.getInputStream());

while((r=bin.read(t))-1){

b=putData(b,t,r);

}

bin.close();

uc.disconnect();

data=b;

}catch(Exception e){}

}

private final byte[] putData(byte[] b, byte[] t, int r) {

byte[] tb=new byte[b.length+r];

System.arraycopy(b, 0, tb, 0, b.length);

System.arraycopy(t, 0, tb, b.length, r);

return tb;

}

}

private static class HttpPost extends Thread{

private static int blen=1024;

private static String contentType="application/x-www-form-urlencoded";

private String url,pms;

private byte[] dat={};

private HttpPost(String u,String p){

url=u;

pms=p;

start();

try{join();}catch(Exception e){}

}

public void run(){

try{

URL u = new URL(url);

HttpURLConnection connection=(HttpURLConnection)u.openConnection();

connection.setRequestMethod("POST");

connection.setRequestProperty("Content-Type",contentType);

connection.setRequestProperty("Content-Length",String.valueOf(pms.length()));

connection.setUseCaches(false);

connection.setDoOutput(true);

connection.setDoInput(true);

DataOutputStream dout=new DataOutputStream(connection.getOutputStream());

dout.write(pms.getBytes());

dout.flush();

dout.close();

InputStream in=connection.getInputStream();

BufferedInputStream bin=new BufferedInputStream(in);

byte[] buff=new byte[blen],bs={};

int r;

while((r=bin.read(buff))-1){

bs=putData(bs,buff,r);

}

bin.close();

connection.disconnect();

dat=bs;

}catch(Exception e){}

}

private final byte[] putData(byte[] b, byte[] t, int r) {

byte[] tb=new byte[b.length+r];

System.arraycopy(b, 0, tb, 0, b.length);

System.arraycopy(t, 0, tb, b.length, r);

return tb;

}

}

}


网站栏目:java伪代码登录 java命令模式伪代码
分享链接:http://dzwzjz.com/article/dshosso.html
在线咨询
服务热线
服务热线:028-86922220
TOP