如何模拟登如网站?

有哪位大哥知道?

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class GetKaixinWang {
public static void main(String[] args) {
String email = "**********";
String password = "*************";
try {
URL url = new URL("http://www.kaixin.com/");
URLConnection urlC = url.openConnection();
HttpURLConnection httpUrlConnection = (HttpURLConnection) urlC;
//设置是否向httpUrlConnection读入
httpUrlConnection.setDoInput(true);
//设置是否向httpUrlConnection输出
httpUrlConnection.setDoOutput(true);
//post请求时,不能使作缓存
httpUrlConnection.setUseCaches(false);
//设定传输的内容是否是可序列化java对象
httpUrlConnection.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 8.0; Windows vista)");
httpUrlConnection.setRequestMethod("POST");
//连接
httpUrlConnection.connect();
// String cookieVal = httpUrlConnection.getHeaderField("Set-Cookie");
// System.out.println(cookieVal);
// String sessionId = "";
// if(cookieVal != null){
// sessionId = cookieVal.substring(cookieVal.indexOf("=")+1, cookieVal.indexOf(";"));
// }
// System.out.println(sessionId);
// httpUrlConnection.setRequestProperty("Cookie", sessionId);
StringBuffer sb = new StringBuffer();
sb.append("email="+email);
sb.append("&password="+password);
sb.append("&login_type="+"1001");

//post信息
OutputStream os = httpUrlConnection.getOutputStream();
os.write(sb.toString().getBytes("utf-8"));
os.close();

BufferedReader br = new BufferedReader(new InputStreamReader(httpUrlConnection.getInputStream()));
String str = br.readLine();
while(str != null){
System.out.println(new String(str.getBytes(),"utf-8"));
str = br.readLine();
}

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}


}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值