关于httpclient访问提转页面的实现

我使用httpclient访问路径 http://localhost:8078/Test/test1111.htm 并打印该页面的html代码
但该test111.htm的代码是跳转到别的页面,如:
[code="html"]

function test(){ location.href = "http://www.baidu.com"; }




[/code]

其实httpclient取出的代码就是该test1111.htm的代码,但该页面其实是直接跳转到www.baidu.com页面
我如果想去www.baidu.com页面的代码该如何实现呢,当然我可以直接使用httpclient访问 www.baidu.com,但在实际运用过程中跳转到那个页面的路径是被加密的,我无法取得,在该例子中就是我只知道我访问的页面会跳转至其他页面,但我无法取得它跳转到得就是www.baidu.com,这个时候我希望httpclient能够延迟一下访问,当然这个只是我的意思,不知道能否实现,而达到取得跳转后页面的html代码,请各位高人给下指点,谢谢,补上httpclient访问的简单代码段,提供大家调试,谢谢
[code="java"]private static void test() throws HttpException, IOException{
HttpClient httpclient = new HttpClient();
httpclient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, CONTENT_CHARSET);
GetMethod getMethod = new GetMethod("http://localhost:8078/Test/test1111.htm");
int statusCode = httpclient.executeMethod(getMethod);
String responseString = new String(getMethod.getResponseBodyAsString().getBytes("gbk"));
System.out.println(responseString);
}[/code]

各位知道的老师请给予指点,感激不尽,分数不多,没办法
[b]问题补充:[/b]
你说执行html中的js代码
问题就在于对方对该js代码采取了压缩,加密...等等很多操作
甚至有自己一套验证方式,想跟踪js代码来获取最后的动态路径是很困难的
有没有其他什么办法呢
[b]问题补充:[/b]
自己解决了 用jdic 不过lovewhzlq的httputil也应该是一个不错的选择 只是没有详细去研究 谢谢了

3个回答

用HttpUnit比较容易一些,至少在执行页面的js方面
http://www.lupaworld.com/328269/viewspace-122286.html

这个就是要执行html的js代码了

[code="java"]/**
notes:
1:get前加上COOKIE
2:get时加上HOST

*/

final String HOST= "http://www.netyi.net ";
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost( "www.netyi.net ", 80, "http ");
client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
PostMethod post = new PostMethod(
"/jsLoginStatus.aspx?returnUrl=%2fjsLoginStatus.aspx%3fguid%3db71cdc08-8622-42de-a4ba-fbd395c032ca ");
// NameValuePair chk = new NameValuePair( "chk " , "yes " );
// NameValuePair re = new NameValuePair( "re_log " , "yes " );
NameValuePair name = new NameValuePair( "UserName ", "zlh218 ");
NameValuePair pass = new NameValuePair( "Password ", "000000 ");
NameValuePair x = new NameValuePair( "Submit.x ", "0 ");
NameValuePair y = new NameValuePair( "Submit.y ", "0 ");
NameValuePair submit = new NameValuePair( "Submit ", "登 录 ");
post.setRequestHeader( "Referer ", "http://netyi.net/jsLoginStatus.aspx ");
post.setRequestHeader( "Content-Type ",
"application/x-www-form-urlencoded ");
post.setRequestHeader( "Content-Length ", "53 ");
post
.setRequestHeader(
"Cookie ",
"Passport=zlh218; domain=.netyi.net; validateCode=88E67E5A0FA31EA4D710A2E00A28C414; domain=.netyi.net; dnt=userid=802856&password=K8UyuJGWSS%2bbJ%2fduDu3plC07d%2fl6D%2b%2bqxIsCtqLlc0oTTWu77t9g3w%3d%3d&tpp=0&ppp=0&pmsound=0&invisible=0&referer=index.aspx&sigstatus=0&expires=-1; dnttemplateid=0; expires=Tue, 11-Aug-2009 14:39:06 GMT; "

  • "dnt=userid=802856&password=K8UyuJGWSS%2bbJ%2fduDu3plC07d%2fl6D%2b%2bqxIsCtqLlc0oTTWu77t9g3w%3d%3d&tpp=0&ppp=0&pmsound=0&invisible=0&referer=index.aspx&sigstatus=0&expires=-1; lastolupdate=1043693968; .ASPXAUTH=6C2E31EECFB08DFF79EA98EAAB54AB30D2CBA7A5CBEB5609312C30989F56CF07DDB84D281E03DD6AF89B4070918C20D25E0195D8F4AB9193E08AA9B0FCA53B16405AFCFD166DBEC7B30340F46E325669; domain=.netyi.net; path=/ ");
    post.setRequestBody(new NameValuePair[] { name, pass, x, y, submit });
    try {
    int status = client.executeMethod(post);
    System.out.println(status);

          //打印服务器返回的状态
    
            System.out.println(post.getStatusLine());
    
            //code=302   redirect   page
    

if (status == HttpStatus.SC_MOVED_TEMPORARILY
|| status == HttpStatus.SC_MOVED_PERMANENTLY) {
Header locationHeader = post.getResponseHeader( "location ");
if (locationHeader != null) {
String redirectUri = locationHeader.getValue();
if (redirectUri == null || " ".equals(redirectUri)) {
redirectUri = "/ ";
}
System.out.println( "redirecturl: "+redirectUri);
// client.getState().setCredentials( "netyi.net ", "realm ",new
// UsernamePasswordCredentials( "zlh218 ", "000000 "));

GetMethod get = new GetMethod(HOST+redirectUri);
get.setDoAuthentication(true);
get
.setRequestHeader(
"Cookie ",
"Passport=zlh218; domain=.netyi.net; validateCode=88E67E5A0FA31EA4D710A2E00A28C414; domain=.netyi.net; dnt=userid=802856&password=K8UyuJGWSS%2bbJ%2fduDu3plC07d%2fl6D%2b%2bqxIsCtqLlc0oTTWu77t9g3w%3d%3d&tpp=0&ppp=0&pmsound=0&invisible=0&referer=index.aspx&sigstatus=0&expires=-1; dnttemplateid=0; expires=Tue, 11-Aug-2009 14:39:06 GMT; "

  • "dnt=userid=802856&password=K8UyuJGWSS%2bbJ%2fduDu3plC07d%2fl6D%2b%2bqxIsCtqLlc0oTTWu77t9g3w%3d%3d&tpp=0&ppp=0&pmsound=0&invisible=0&referer=index.aspx&sigstatus=0&expires=-1; lastolupdate=1043693968; .ASPXAUTH=6C2E31EECFB08DFF79EA98EAAB54AB30D2CBA7A5CBEB5609312C30989F56CF07DDB84D281E03DD6AF89B4070918C20D25E0195D8F4AB9193E08AA9B0FCA53B16405AFCFD166DBEC7B30340F46E325669; domain=.netyi.net; path=/ "); client.executeMethod(get); System.err.println( "get redirect: "); Header[] headers = get.getResponseHeaders(); for (int i = 0; i < headers.length; ++i) { System.err.println(headers[i].toString()); } BufferedReader bf = new BufferedReader( new InputStreamReader(get.getResponseBodyAsStream())); String s = null; while ((s = bf.readLine()) != null) System.out.println(s);

get.releaseConnection();
}
}
} catch (Exception e) {
e.printStackTrace();
}

}[/code]

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐