java模拟post请求登录猫扑示例分享
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("登录失败");
}
}
}
上一篇: Java通过索引值实现约瑟夫环算法
下一篇: Python实现视频下载功能