java post 登陆_java模拟post请求登录

importjava.io.BufferedReader;importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.io.OutputStreamWriter;importjava.net.HttpURLConnection;importjava.net.URL;importjava.util.Scanner;importjava.util.StringTokenizer;importorg.jsoup.Jsoup;importorg.jsoup.nodes.Document;importorg.jsoup.select.Elements;public classTestPost {public static void main(String args[]) throwsIOException{

Scanner scanner= newScanner(System.in);

System.out.println("请输入用户名:");

String user_name=scanner.next();

System.out.println("请输入密码:");

String password=scanner.next();

testPost(user_name , password,"d:/fileDown2.txt");

testJsoup();

}public static void testPost(String user_name , String password,String outPath) throwsIOException{

String login="";

URL url= new URL("http://passport.mop.com");

HttpURLConnection connection= null;

connection= (HttpURLConnection) url.openConnection();//建立链接

connection.setInstanceFollowRedirects(false);

connection.setRequestProperty("Connection","keep-alive");

connection.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36");

connection.addRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

connection.setDoInput(true);

connection.setDoOutput(true);//connection.disconnect();

String str = connection.getHeaderField("Location");//获得重定向的url地址

URL newURL = newURL(str);//String cookies = getCookies(connection);

HttpURLConnection conn =(HttpURLConnection) newURL.openConnection();

conn.setRequestProperty("Referer", str);//浏览器向 WEB 服务器表明自己是从哪个 网页/URL 获得/点击 当前请求中的网址/URL//conn.setRequestProperty("Cookie", cookies);//发送设置cookie:

conn.setRequestProperty("Connection","keep-alive");

conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36");

conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");

conn.setDoInput(true);

conn.setDoOutput(true);

OutputStreamWriter out= new OutputStreamWriter(conn.getOutputStream(),"utf-8");

login=login+"user_name="+user_name+"&"+"password="+password;

out.write(login);

out.flush();

out.close();

InputStream inputStream=conn.getInputStream();

BufferedReader reader= new BufferedReader(new InputStreamReader(inputStream, "utf-8"));

reader.close();//链接到personal页面

String headerName = null;

StringBuilder myCookies= newStringBuilder();//myCookies.append(cookies + ";");

for(int i =1;(headerName= conn.getHeaderFieldKey(i))!=null;i++){if(headerName.equals("Set-Cookie")){

String cookie=conn.getHeaderField(i);

cookie= cookie.substring(0, cookie.indexOf(";"));

String cookieName= cookie.substring(0, cookie.indexOf("="));

String cookieValue= cookie.substring(cookie.indexOf("=") + 1, cookie.length());

myCookies.append(cookieName+ "=");

myCookies.append(cookieValue+ ";");

}

}

URL purl= new URL("http://passport.mop.com/personal");

HttpURLConnection pconn=(HttpURLConnection) purl.openConnection();

pconn.setRequestProperty("Referer", str);

pconn.setRequestProperty("Cookie", myCookies.toString());

pconn.connect();

InputStream inputStream1=pconn.getInputStream();//BufferedReader reader1 = new BufferedReader(new InputStreamReader(inputStream1,"utf-8"));//String line1 = reader1.readLine();//while(line1 != null){//System.out.println(line1);//line1 = reader1.readLine();//}//reader1.close();

int chByte = 0;

FileOutputStream fileOut= new FileOutputStream(newFile(outPath));

chByte=inputStream1.read();while(chByte != -1){

fileOut.write(chByte);

chByte=inputStream1.read();

}

}private staticString getCookies(HttpURLConnection conn) {//TODO Auto-generated method stub//StringBuffer cookies = new StringBuffer();

StringBuilder cookies = newStringBuilder();

String headName;for (int i = 1; (headName = conn.getHeaderField(i)) != null; i++) {

StringTokenizer st= new StringTokenizer(headName, "; ");while(st.hasMoreTokens()) {

cookies.append(st.nextToken()+ "; ");

}

}returncookies.toString();

}private static void testJsoup() throwsIOException{//解析html文档

File input = new File("D:/fileDown2.txt");

Document doc= Jsoup.parse(input, "UTF-8");//for(Element ele : doc.getElementsByClass("zhnc").select("ul")){//if(!ele.select("li").toString().equals("")){//String text = ele.select("li").text();//System.out.println("user_name is:"+text);//}//}

Elements ele = doc.getElementsByClass("zhnc").select("ul");if(!ele.select("li").toString().equals("")){

String text= ele.select("li").text();

System.out.println("user_name is:"+text);

}else{

System.out.println("登录失败");

}

}

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值