1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
import java.io.File; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import org.apache.commons.io.FileUtils; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.cookie.Cookie; import org.apache.http.entity.BufferedHttpEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; public class KaiXin001 { public InputStream getResourceAsStream(String filename) throws Exception { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); InputStream in = null; if (classLoader != null) { in = classLoader.getResourceAsStream(filename); } if (in == null) { in = ClassLoader.getSystemResourceAsStream(filename); } if (in == null) { throw new Exception("Can't find resource file: " + filename); } else { return in; } } public void writeToFile(String file, HttpEntity entity) throws Exception { writeToFile(file, EntityUtils.toString(entity)); } public void writeToFile(String file, String data) throws Exception { FileUtils.writeStringToFile(new File(file), data, "UTF-8"); } public String getContent(HttpEntity entity) throws Exception { if (entity != null) { entity = new BufferedHttpEntity(entity); long len = entity.getContentLength(); System.out.println("Length: " + len); System.out.println("====================\r\n"); return EntityUtils.toString(entity, "UTF-8"); } else { System.out.println("entity is null."); return null; } } public void setCookie(DefaultHttpClient httpclient, List<Cookie> cookies) { if (cookies.isEmpty()) { System.out.println("Cookie is empty."); return; } else { for (int i = 0; i < cookies.size(); i++) { System.out.println((i + 1) + " - " + cookies.get(i).toString()); httpclient.getCookieStore().addCookie(cookies.get(i)); } System.out.println(); } }
public String enterComponentContent(String url, String componentName, DefaultHttpClient httpclient, List<Cookie> cookies, HttpResponse response, HttpEntity entity) throws Exception { System.out.println("--- Enter: " + componentName + " ---"); System.out.println("--- Url: " + url + " ---"); setCookie(httpclient, cookies); HttpGet httpget = new HttpGet(url); response = httpclient.execute(httpget); entity = response.getEntity(); return getContent(entity); } public void showResponseStatus(HttpResponse response) {
System.out.println(response.getStatusLine().toString()); System.out.println(" ---\r\n"); } public static void main(String[] args) throws Exception { KaiXin001 kx = new KaiXin001(); DefaultHttpClient httpclient = new DefaultHttpClient(); HttpPost httpost = new HttpPost("http://www.kaixin001.com/login/login.php"); List<NameValuePair> qparams = new ArrayList<NameValuePair>(); qparams.add(new BasicNameValuePair("email", "email")); qparams.add(new BasicNameValuePair("password", "password")); httpost.setEntity(new UrlEncodedFormEntity(qparams, HTTP.UTF_8)); HttpResponse response = httpclient.execute(httpost); kx.showResponseStatus(response);
HttpEntity entity = response.getEntity(); kx.getContent(entity);
List<Cookie> cookies = httpclient.getCookieStore().getCookies(); System.out.println("Post logon cookies:"); kx.setCookie(httpclient, cookies);
String homepage = "http://www.kaixin001.com/home/"; String content = null; content = kx.enterComponentContent(homepage, "Home page", httpclient, cookies, response, entity);
String componet = "http://www.kaixin001.com/!rich/market.php"; content = kx.enterComponentContent(componet, "Component", httpclient, cookies, response, entity);
--- kx.writeToFile("c:/kaixin.html", content);
httpclient.getConnectionManager().shutdown(); } }
|