62,614
社区成员
发帖
与我相关
我的任务
分享
null
HTTP/1.1 200 OK
Content-Language
cn
Date
Mon, 29 Sep 2008 03:58:04 GMT
Vary
Accept-Encoding
Transfer-Encoding
chunked
Expires
Mon, 29 Sep 2008 04:58:04 GMT
Keep-Alive
timeout=15, max=60
Connection
Keep-Alive
Content-Type
text/html; charset=GB2312
Server
Apache
Cache-Control
max-age=3600
//import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Test {
public static void main(String[] args) {
try {
URL url = new URL("http://www.taobao.com");
HttpURLConnection huc = (HttpURLConnection)url.openConnection();
huc.connect();
//InputStream is = huc.getInputStream();
//取得sessionID
String cookieVal = huc.getHeaderField("Set-Cookie");
String sessionId = "";
if (cookieVal != null) {
sessionId = cookieVal.substring(0, cookieVal.indexOf(";"));
}
System.out.println(sessionId);
////////////////////////////////////////////////////////////////////////////
Map<String, List<String>> map = huc.getHeaderFields();
Set<String> keys = map.keySet();
for (String key : keys) {
System.out.println(key);
List<String> values = map.get(key);
for (String value : values) {
System.out.println("\t" + value);
}
}
////////////////////////////////////////////////////////////////////////////
} catch (Exception e) {
e.printStackTrace();
}
}
}
if (cookieVal != null) {
sessionId = cookieVal.substring(0, cookieVal.indexOf(";"));
}
import java.io.IOException;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
public class Main {
public static final String USER_AGENT = "Mozilla/5.0 (Windows; U; Windows NT 6.0; zh-CN; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1";
public static void main(String[] args) {
String username = "YourUsername";
String password = "YourPassword";
try {
HttpClient hc = new HttpClient();
hc.getParams().setParameter(HttpClientParams.USER_AGENT, USER_AGENT);
hc.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
////////////////////////////////////////////////////////////////////////////
GetMethod hm1 = new GetMethod("http://reg.hexun.com/login.aspx");
hc.executeMethod(hm1);
hm1.releaseConnection();
if (hm1.getStatusCode() != HttpStatus.SC_OK) return;
////////////////////////////////////////////////////////////////////////////
PostMethod hm2 = new PostMethod("http://reg.hexun.com/login.aspx");
hm2.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
hm2.addParameter("TextBoxUserName", username);
hm2.addParameter("TextBoxPassword", password);
hm2.addParameter("TextGpic", "");
hm2.addParameter("hiddenReferrer", "http://blog.hexun.com/");
hm2.addParameter("gourl", "");
hm2.addParameter("LoginStateName", "1");
hm2.addParameter("submitsign", "1");
hm2.addParameter("x", "32");
hm2.addParameter("y", "9");
hc.executeMethod(hm2);
hm2.releaseConnection();
if (hm2.getStatusCode() != HttpStatus.SC_MOVED_TEMPORARILY) return;
////////////////////////////////////////////////////////////////////////////
String rdurl = hm2.getResponseHeader("Location").getValue();
StringBuilder sb = new StringBuilder();
String tck = null;
Cookie[] cks = hc.getState().getCookies();
for (Cookie ck : cks) {
if ("ASP.NET_SessionId".equalsIgnoreCase(ck.getName())) continue;
if ("hxck_sq_common".equalsIgnoreCase(ck.getName())) tck = ck.getValue();
if (sb.length() > 0) sb.append("; ");
sb.append(ck.getName());
sb.append("=");
sb.append(ck.getValue());
}
hc.getState().clearCookies();
////////////////////////////////////////////////////////////////////////////
GetMethod hm3 = new GetMethod(rdurl);
hm3.setFollowRedirects(false);
hm3.addRequestHeader("Referer", "http://reg.hexun.com/login.aspx");
hm3.addRequestHeader("Cookie", sb.toString());
hc.executeMethod(hm3);
hm3.releaseConnection();
if (hm3.getStatusCode() != HttpStatus.SC_MOVED_PERMANENTLY) return;
////////////////////////////////////////////////////////////////////////////
rdurl = hm3.getResponseHeader("Location").getValue();
sb.setLength(0);
cks = hc.getState().getCookies();
for (Cookie ck : cks) {
if ("ASP.NET_SessionId".equalsIgnoreCase(ck.getName())) continue;
if (sb.length() > 0) sb.append("; ");
sb.append(ck.getName());
sb.append("=");
sb.append(ck.getValue());
}
if (sb.length() > 0) sb.append("; ");
sb.append("; hxck_sq_common=");
sb.append(tck);
hc.getState().clearCookies();
////////////////////////////////////////////////////////////////////////////
GetMethod hm4 = new GetMethod(rdurl);
hm4.setFollowRedirects(false);
hm4.addRequestHeader("Referer", "http://reg.hexun.com/login.aspx");
hm4.addRequestHeader("Cookie", sb.toString());
hc.executeMethod(hm4);
System.out.println(hm4.getResponseBodyAsString());
hm4.releaseConnection();
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}