ccjsjymg 发表于 2013-1-28 19:33:41

如何模拟登如网站?

有哪位大哥知道?
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]
查看完整版本: 如何模拟登如网站?