java模拟post请求登录猫扑示例分享_java

复制代码 代码如下:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;
import java.util.StringTokenizer;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class TestPost {
 public static void main(String args[]) throws IOException{ 
  Scanner scanner = new Scanner(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) throws IOException{
  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 = new URL(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 = new StringBuilder();
//  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(new File(outPath));
  chByte = inputStream1.read();
  while(chByte != -1){
   fileOut.write(chByte);
   chByte = inputStream1.read();
  }
 }
 private static String getCookies(HttpURLConnection conn) {
  // TODO Auto-generated method stub
//     StringBuffer cookies = new StringBuffer();  
     StringBuilder cookies = new StringBuilder();
           String headName;  

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

               StringTokenizer st = new StringTokenizer(headName, "; ");  

               while (st.hasMoreTokens()) {  

                   cookies.append(st.nextToken() + "; ");  
               }  
           }             
           return cookies.toString();      
 }

 private static void testJsoup() throws IOException{
  //解析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("登录失败");
     }
 }
}

时间: 2024-11-02 09:30:33

java模拟post请求登录猫扑示例分享_java的相关文章

java基于AspectJ(面向切面编程)编码示例分享_java

一.基本概念 AspectJ是一种面向切面程序设计的基于Java 的实现.它向 Java 中加入了连接点(Join Point)这个新概念,其实它也只是现存的一个 Java概念的名称而已.它向 Java 语言中加入少许新结构:切点(pointcut).通知(Advice).类型间声明(Inter-type declaration)和方面(Aspect).切点和通知动态地影响程序流程,类型间声明则是静态的影响程序的类等级结构,而切面则是对所有这些新结构的封装. 基于切面.连接点.切点.通知的概念如

java动态代理和cglib动态代理示例分享_java

java动态代理类可以分为两种. 静态代理:由程序员创建或特定工具自动生成源代码,再对其编译.在程序运行前,代理类的.class文件就已经存在了. 动态代理:在程序运行时,运用反射机制动态创建而成. 一.首先我们进行java动态代理的演示. 现在我们有一个简单的业务接口Saying,如下: 复制代码 代码如下: package testAOP;public interface Saying {public void sayHello(String name);    public void ta

java读取csv文件和写csv示例分享_java

复制代码 代码如下: import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.

java删除文件夹下所有文件示例分享_java

复制代码 代码如下: package org.sw; import java.io.File; public class DeleteFolder {     /**     * 删除文件夹下的所有文件     * @param oldPath     */    public void deleteFile(File oldPath) {          if (oldPath.isDirectory()) {           System.out.println(oldPath + "

java配置dbcp连接池(数据库连接池)示例分享_java

使用jar包:commons-dbcp-1.4.jar.commons-pool-1.6.jar.commons-logging-1.1.3.jar,另外还有数据库的jdbc驱动,适用java6及以上平台 连接池管理类 复制代码 代码如下: package cn.songxinqiang.samples.commonsdbcp.util; import java.sql.Connection;import java.sql.DatabaseMetaData;import java.sql.SQL

java解析xml之sax解析xml示例分享_java

复制代码 代码如下: package com.test; import java.io.File;import java.io.FileInputStream;import java.util.ArrayList;import java.util.List; import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes;import org.x

java解析xml之dom解析xml示例分享_java

复制代码 代码如下: package com.test; import java.io.File;import java.util.ArrayList;import java.util.List;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;import org.w3c.dom.Element;import o

java解析xml之dom4j解析xml示例分享_java

复制代码 代码如下: package com.test; import java.io.File;import java.util.ArrayList;import java.util.Iterator;import java.util.List; import org.dom4j.Document;import org.dom4j.Element;import org.dom4j.io.SAXReader; public class Dom4jXML {     public static v

java设计模式之实现对象池模式示例分享_java

ObjectPool抽象父类 复制代码 代码如下: import java.util.Iterator;import java.util.Vector; public abstract class ObjectPool<T> {    private Vector<T> locked, unlocked;   // locked是已占用的对象集合,unlocked是可用对象集合    public ObjectPool() {    locked = new Vector<T