复制代码代码如下:
importar java.io.bufferedReader;
importar java.io.file;
importar java.io.fileOutputStream;
importar java.io.ioException;
importar java.io.inputStream;
importar java.io.inputStreamReader;
importar java.io.OutputStreamWriter;
importar java.net.httpurlConnection;
importar java.net.url;
importar java.util.scanner;
importar java.util.stringTokenizer;
importar org.jsoup.jsoup;
importar org.jsoup.nodes.document;
importar org.jsoup.select.Elements;
classe pública TestPost {
public static void main (string args []) lança ioexception {
Scanner Scanner = New Scanner (System.in);
System.out.println ("请输入用户名 ::");
String user_name = scanner.next ();
System.out.println ("请输入密码 ::");
String senha = scanner.Next ();
testpost (user_name, senha, "d: /FileDown2.txt");
testJSoup ();
}
public static void testPost (string user_name, string senha, string outpath) lança ioexception {
String login = "";
Url url = new url ("http://passport.mop.com");
Conexão httpurlConnection = null;
conexão = (httpurlConnection) url.openconnection (); // 建立链接
conexão.SetInstanceFollowDredirects (false);
Connection.SetRequestProperty ("Connection", "Keep-Alive");
Connection.SetRequestProperty ("User-Agent",
"Mozilla/5.0 (Windows NT 6.1; Wow64) Applewebkit/537.36 (Khtml, como 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 ("Local"); // 获得重定向的 url 地址
Url newurl = novo url (str);
// String Cookies = getCookies (conexão);
HttpurlConnection Conn = (httpurlConnection) newurl.openconnection ();
Conn.setRequestProperty ("Referente", STR); // Web 服务器表明自己是从哪个 网页/url 获得/点击 当前请求中的网址/url
//conn.setRequestProperty("cookie ", cookies); // biscoito:
Conn.SetRequestProperty ("Connection", "Keep-Alive");
Conn.SetRequestProperty ("User-Agent",
"Mozilla/5.0 (Windows NT 6.1; Wow64) Applewebkit/537.36 (Khtml, como 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+"&"+"senha ="+senha;
out.write (login);
out.flush ();
out.Close ();
InputStream inputStream = Conn.getInputStream ();
BUBLEREDREADER LEITOR = NEW BUBLESEDRADER (new InputStreamReader (InputStream, "UTF-8"));
leitor.close ();
// 链接到 PESSOAL 页面
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 ("="));
CookieValue de cordas = 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 ("Referente", STR);
pconn.setRequestProperty ("cookie", mycookies.toString ());
pconn.connect ();
InputStream inputStream1 = pconn.getInputStream ();
// bufferredreader leitor1 = new buffarreder (new inputStreamReader (inputStream1, "utf-8"));
// string line1 = reader1.readline ();
// while (line1! = null) {
//System.out.println(Line1);
// linha1 = leitor1.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 ();
}
}
String estática privada getcookies (httpurlConnection conn) {
// TODO 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 (nome da cabeça, ";");
while (St.HasmoreTokens ()) {
Cookies.Append (St.NextToken () + ";");
}
}
retornar cookies.toString ();
}
private static void testjSoup () lança IoException {
// 解析 html 文档
Entrada de arquivo = novo arquivo ("D: /FiledOwn2.txt");
Documento doc = jsoup.parse (entrada, "utf-8");
// para (elemento ele: doc.getElementsbyclass ("zhnc"). select ("ul")) {
// if (! ele.select ("li"). ToString (). Equals ("")) {
// text string = ele.select ("li"). Text ();
// system.out.println ("user_name é:"+text);
//}
//}
Elementos ele = doc.getElementsByClass ("zhnc"). Select ("ul");
if (! Ele.Select ("li"). ToString (). Equals ("")) {
Text string = ele.select ("li"). Text ();
System.out.println ("user_name é:"+text);
}outro{
System.out.println ("登录失败");
}
}
}