`
ccjsjymg
  • 浏览: 60667 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

如何模拟登如网站?

阅读更多
有哪位大哥知道?
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();
		}
		
		
	}
	
}
分享到:
评论
1 楼 volking 2011-07-19  
其他网站好像不行。。。

相关推荐

Global site tag (gtag.js) - Google Analytics