我现在有这么个需求要用httpclient访问个网页 网页插入了计数器 但是我用httpclient访问这个页面 计数器是不变的我做了一些小测试用httpclient访问这个页面但是计数器是不变的 我想问下httpclient怎么能像IE 这样访问这个页面然后计数器就会统计页面访问量找了两天也没找出怎么回事是因为协议还是别的看了httpclient帮助说httpclient能模拟浏览器的 不知道从那下手了 请大家帮助下 小弟先谢谢了 分不多不好意思大家帮帮忙 我在页面中的记数器 不知道它是怎么记录页面的访问量的 看JS好象是访问Cookie JS代码发下 大家帮我看下 谢谢!~<script type="text/javascript">
<!--
var expireDate=new Date();
var hours=expireDate.getHours();
var minutes=expireDate.getMinutes();
var seconds=expireDate.getSeconds();
var now=expireDate.getTime();
function getCookieVal_cnzz (offset)
{
var endstr = document.cookie.indexOf(";",offset);
if(endstr==-1){
endstr=document.cookie.length;
}
return unescape(document.cookie.substring(offset,endstr));
}
function GetCookie_cnzz(name){
var arg=name+"=";
var alen=arg.length;
var clen=document.cookie.length;
var i=0;
while(i< clen){
var j=i+alen;
if(document.cookie.substring(i,j)==arg)
{
return getCookieVal_cnzz(j);
}
i=document.cookie.indexOf(" ",i)+1;
if(i==0)
break;
}
return null;
}
var agt=navigator.userAgent.toLowerCase();
data='&agt='+escape(agt)+'&r='+escape(document.referrer)+ '&aN='+escape(navigator.appName)+'&lg='+escape(navigator.systemLanguage) + '&OS=' + escape(navigator.platform)+'&aV='+escape(navigator.appVersion)+'&ntime=0.68975600 1182259556';
cnzz_a=GetCookie_cnzz("cnzz02");
if(cnzz_a!=null){
cnzz_a=parseInt(cnzz_a);
cnzz_a=cnzz_a+1;
}else{
cnzz_a=0;
}
data=data+'&repeatip='+cnzz_a;
rtime=GetCookie_cnzz("rtime");
ltime=GetCookie_cnzz("ltime");
cnzz_eid=GetCookie_cnzz("cnzz_eid");
if(cnzz_eid == null){
cnzz_eid=Math.floor(Math.random()*100000000)+"-"+document.referrer;
}
if(ltime< 1000000){
rtime=0;
ltime=0;
}else{
rtime=parseInt(rtime)
};
if(rtime< 1){
rtime=0
};
ltime=parseInt(ltime);
now=parseInt(now);
if(((now-ltime)>43200*1000)&&(ltime>0)){rtime=rtime+1};
data=data+'&rtime='+rtime+'&cnzz_eid='+escape(cnzz_eid);
data=data+'&showp='+escape(screen.width+'x'+screen.height);
 document.write('站长统计'); document.write('');
  var lefttime=1000*(86400-hours*3600-minutes*60-seconds);
  expireDate.setTime(expireDate.getTime()+500*86400);
  document.cookie="cnzz02="+cnzz_a+";expires="+expireDate.toGMTString()+ ";path=/";
   var lefttime=1000*86400*182;
   expireDate.setTime(now+lefttime);
   document.cookie="rtime="+rtime+";expires="+expireDate.toGMTString()+";path=/";
   document.cookie="ltime="+now+";expires="+expireDate.toGMTString()+";path=/";
   document.cookie="cnzz_eid="+escape(cnzz_eid)+";
   expires="+expireDate.toGMTString()+";
   path=/";
//-->
</script>

解决方案 »

  1.   

    这个……
    如果你不想编写一个javascript解释器的话,只好根据每个带计数器的页面编写一段程序了……
    在你的请求头中带上cookie,具体值根据脚本中的设置cookie那部分来写;然后请求一下这个计数器的页面,应该就行了。
      

  2.   

    那怎么用httpclient 生成cookie呢 我第一次过去的时候cookie什么都没有不会设置cookie 我的代码
    mport java.io.IOException;import org.apache.commons.httpclient.Cookie;
    import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.HttpException;
    import org.apache.commons.httpclient.HttpStatus;
    import org.apache.commons.httpclient.methods.GetMethod;
    import org.apache.commons.httpclient.params.HttpMethodParams;public class HttpClientTutorial {
      
      private static String url = "http://localhost:8088/adsadmin/iframe.jsp";  public static void main(String[] args) {
        // 创建 HttpClient 的实例.
        HttpClient client = new HttpClient();
        //client.getHostConfiguration().setHost( " http://localhost" ,8088 ,"http" );    //创建某种连接方法的实例,在这里是 GetMethod。在 GetMethod 的构造函数中传入待连接的地址
        GetMethod method = new GetMethod(url);    client.getParams().setParameter("http.useragent",
    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
        client.getParams().setParameter("http.Connection",
    "Keep-Alive");
        
        // 准备订制 
        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 
         new DefaultHttpMethodRetryHandler(3, false));
        
        try {
          // 返回值是一个整数,表示了执行该方法后服务器返回的状态码
          int statusCode = client.executeMethod(method);      if (statusCode != HttpStatus.SC_OK) {
            System.err.println("Method failed: " + method.getStatusLine());
          }      // Read the response body.
          byte[] responseBody = method.getResponseBody();      // Deal with the response.
          // Use caution: ensure correct character encoding and is not binary data
          System.out.println(new String(responseBody));
          Cookie[] cookies = client.getState().getCookies();
          //得到cookies
          System.out.println("Present cookies: ");
          for (int i = 0; i < cookies.length; i++) {//循环结构零部件   
              System.out.println(" - " + cookies[i].toExternalForm());   
              System.out.println(" - domain=" + cookies[i].getDomain());   
              System.out.println(" - path=" + cookies[i].getPath()); 
              System.out.println(" - Version=" + cookies[i].getVersion());
              System.out.println(" - Comment="+cookies[i].getComment());
              System.out.println(" - Value="+cookies[i].getValue());
              System.out.println(" - ExpiryDate="+cookies[i].getExpiryDate());
              System.out.println(" - Secure="+cookies[i].getSecure());
          }
         
        } catch (HttpException e) {
          System.err.println("Fatal protocol violation: " + e.getMessage());
          e.printStackTrace();
        } catch (IOException e) {
          System.err.println("Fatal transport error: " + e.getMessage());
          e.printStackTrace();
        } finally {
          // Release the connection.
          method.releaseConnection();
        }    }
    }
      

  3.   

    在httpClient第一次请求怎么带cookie生成一个cookie过去呢?
    看这个javascript还需要head头 头大了 楼上的朋友帮忙给说下 谢谢
      

  4.   

    第一次请求是不可能带有生成的cookie过去的,之后第一次请求之后,服务器为客户端生成session,写回cookie之后,第二次你才可以带一个cookie的。
      

  5.   

    httpClient.getState().setCookiePolicy(CookiePolicy.COMPATIBILITY);能自动处理cookie/session
      

  6.   

    使用CapExpert截包软件截取IE发出去的包,之后用HttpClient去模拟出来,在请求头部肯定有setCookie这一属性,而这cookie是由JS生成,你可用JAVA模拟,或者用JDK1.6执行一下,看他的JS生成了些什么东西,之后再模拟,之后就往client设就OK了,client.setRequestHeader("setCookie","模拟出来的cookie");