下面程序想自动登陆系统:
public static void main(String[] args) throws Exception{   
      HttpClient client = new HttpClient();
      client.getHostConfiguration().setHost("127.0.0.1", 80,"http");
      client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
      PostMethod post = new PostMethod("/login/login.asp");
      NameValuePair name = new NameValuePair("loginname", "[email protected]");    
      NameValuePair pass = new NameValuePair("password", "123456");
      NameValuePair int_count = new NameValuePair("int_count", "999");
      NameValuePair bkurl = new NameValuePair("bkurl=", "");
      post.setRequestHeader("Referer", "http://127.0.0.1/login/login.asp");
      post.setRequestHeader("ContentType", "application/x-www-form-urlencoded");
      post.setRequestHeader("Content-Length", "62");              
      post.setRequestHeader("Cookie", "JSsUserInfo=382C3D7551694479056D1D79446C58754A345C2C4E755D694E79786D6279486C02751034032C1E751F6910795B6D6C79446C5F754A34432C1A75036918790F6D79793B6C54754234232C377551694E79606D6179486C52753C34202C447507691C795A6D4B79066C0C7516342D2C48755A6946791A6D4F791A6C047542343E2C2D75516944790F6D6D79216C54754C34402C487555695579056D19794F6C5B754834562C3A7528694879046D1779206C3D7544345C2C427539693479096D6679276C587548345C2C48755F694479036D1D79436C52753834212C44755C694E798;");              post.setRequestBody(new NameValuePair[]{name,pass,int_count,bkurl});
      int statuscode = client.executeMethod(post);
      if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) ||
                (statuscode == HttpStatus.SC_MOVED_PERMANENTLY) ||
                (statuscode == HttpStatus.SC_SEE_OTHER) ||
                (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)){
      Header header = post.getResponseHeader("location");
      if (header != null) {
              String newurl = header.getValue();
              if ((newurl == null) || (newurl.equals("")))
                  newurl = "/";
              GetMethod get = new GetMethod(newurl);
              client.executeMethod(get);
              BufferedReader bf = new BufferedReader(new InputStreamReader(get.getResponseBodyAsStream()));
              String s = null;
              while ((s=bf.readLine()) != null){                  
               System.out.println(s);
              }
              get.releaseConnection();              
          } else{
              System.out.println("Invalid redirect");
          } 
      post.releaseConnection();
      }else{
       System.out.println("ok");  
      }      
  }
通过ieHTTPHeaders看到以下信息:
POST /login/login.asp?DYWE=1210926948718.119132.1211187954.1211188187.8 HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Referer: http://127.0.0.1/login/login.asp
Accept-Language: zh-cn
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
Host: 127.0.0.1
Content-Length: 62
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: strloginusertype=4; __zpWAM=1210926948718.119132.1211187954.1211188187.8; firstchannelurl=http%3A//127.0.0.1/login/login.asp%3FBkUrl%3D%252Fmyzhaopin%252Fresume%255Fnav%252Easp%253FDYWE%253D1210926948718%252E119132%252E1210927448%252E1211158566%252E3%2526firstRef%253D%252D; lastchannelurl=; JSShowname=kingseo%40163%2Ecom; JSloginnamecookieindex=kingseo%40163%2Ecom; myzl111113171=0; __zpWAMs1=1; [email protected]&password=123456&int_count=999&bkurl=
HTTP/1.0 302 Moved Temporarily
Date: Mon, 19 May 2008 09:14:22 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
Location: /person/resume_index.asp?t=62062.7
Content-Length: 158
Content-Type: text/html; Charset=utf-8
Expires: Mon, 19 May 2008 09:14:22 GMT
Set-Cookie: JSpUserInfo=; domain=127.0.0.1; path=/
Set-Cookie: JSsUserInfo=342C713654365A611D350769076B502C063658365C611D350D697A6B2F2C08360236026142355169456B042C5A3629365A611A350569186B022C5A3604365061793578690B6B5A2C7B3627365661173562697B6B5C2C0E362C36266111355D695F6B0F2C52361A360E6143357669076B572C06364736086143355B690D6B322C613654365A6117357769626B5C2C003644365A6115351669076B562C0F3658365F6117357569726B5C2C053652363E6178350B69076B5A2C60362836566166356469076B502C0436583658611D350169076B572C0E3628362761113506690D6B9; domain=127.0.0.1; path=/
Set-Cookie: JSShowname=kingseo%40163%2Ecom; expires=Tue, 31-Dec-2019 16:00:00 GMT; domain=127.0.0.1; path=/
Set-Cookie: JSloginnamecookieindex=kingseo%40163%2Ecom; expires=Tue, 31-Dec-2019 16:00:00 GMT; domain=127.0.0.1; path=/
Set-Cookie: strloginusertype=1; domain=127.0.0.1; path=/
Cache-Control: private
X-Cache: MISS from web-s57.127.0.0.1
X-Cache-Lookup: MISS from web-s57.127.0.0.1:80
Connection: keep-aliveGET /person/resume_index.asp?t=62062.7 HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Referer: http://127.0.0.1/login/login.asp
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
Host: 127.0.0.1
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: strloginusertype=1; __zpWAM=1210926948718.119132.1211187954.1211188187.8; firstchannelurl=http%3A//127.0.0.1/login/login.asp%3FBkUrl%3D%252Fmyzhaopin%252Fresume%255Fnav%252Easp%253FDYWE%253D1210926948718%252E119132%252E1210927448%252E1211158566%252E3%2526firstRef%253D%252D; lastchannelurl=; JSShowname=kingseo%40163%2Ecom; JSloginnamecookieindex=kingseo%40163%2Ecom; myzl111113171=0; __zpWAMs1=1; __zpWAMs2=1; JSpUserInfo=; JSsUserInfo=342C713654365A611D350769076B502C063658365C611D350D697A6B2F2C08360236026142355169456B042C5A3629365A611A350569186B022C5A3604365061793578690B6B5A2C7B3627365661173562697B6B5C2C0E362C36266111355D695F6B0F2C52361A360E6143357669076B572C06364736086143355B690D6B322C613654365A6117357769626B5C2C003644365A6115351669076B562C0F3658365F6117357569726B5C2C053652363E6178350B69076B5A2C60362836566166356469076B502C0436583658611D350169076B572C0E3628362761113506690D6B9HTTP/1.0 200 OK
Date: Mon, 19 May 2008 09:14:22 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
Content-Type: text/html; Charset=utf-8
Set-Cookie: JSsUserInfo=347320664E73566A466559614264577340675B77526850735F663F73296A4A6503611A6408731467197700680E7324664273516A44654661106409731E67517730682F7359664873296A3965556148642473376757775E6823732A664E735C6A236525614E645D733667277758680A730D661D73006A04650D611C64267342675C7756684F7307661C730A6A4C653B6127645B734267517724683573596646734A6A466551615364577344675077546855735F663773236A4A655361376428734E67517729682C7359664873246A3365556143645D7332672677586851735F662673266A4A6522612164577342675B775468527355664473566A41655361306427734E6751772168207359664873326A2365556142645D732; domain=127.0.0.1; path=/
Set-Cookie: monitorlogin=Y; path=/
Set-Cookie: strloginusertype=4; expires=Tue, 31-Dec-2019 16:00:00 GMT; domain=127.0.0.1; path=/
Cache-Control: private
X-Cache: MISS from web-s46.127.0.0.1
X-Cache-Lookup: MISS from web-s46.127.0.0.1:80
Via: 1.0 web-s46.127.0.0.1 (squid/3.0.STABLE4)
Connection: keep-alive
Content-Encoding: gzip
Content-Length: 3477

解决方案 »

  1.   

    系统输出以下信息:
    错误:您所请求的网址(URL)无法获取
    您所请求的网址(URL)无法获取
    当尝试进行以下请求时:
    GET resume_index.asp HTTP 1.1
    User Agent: Jakarta Commons HttpClient 3.1
    Host: 127.0.0.1
    Cookie: [email protected]
    Cookie: [email protected]
    Cookie: strloginusertype=1
    Cookie: JSsUserInfo=347320664E73566A466559614264577340675B77526850735F663F73296A4A6503611A6408731....
    Connection: Keep Alive
    X Forwarded For: 218.14.55.185  
    发生了下列的错误:
    无效的请求
    Some aspect of the HTTP Request is invalid.  Possible problems:
    HTTP 请求的某些方面是无效的。可能是下列问题:
    1、Missing or unknown request method--缺少请求方式或未知的请求方式
    2、Missing URL--缺少网址
    3、Missing HTTP Identifier (HTTP 1.0)--缺少 HTTP 标识(HTTP 1.0)
    4、Request is too large--请求命令过长
    5、Content-Length missing for POST or PUT requests--POST 或 PUT 请求缺少内容长度
    6、Illegal character in hostname; underscores are not allowed--主机名称中包含不合法的字符;下划线是不允许的。本缓存服务器管理员:webmaster. 
      

  2.   

    请各位高手帮忙帮忙,小弟在此先谢过各位的帮忙,thanks!
      

  3.   

    您所请求的网址(URL)无法获取 
    用浏览器访问的话,地址是多少?
      

  4.   

    http://127.0.0.1/login/login.asp 处理登陆
    登陆后,转到http://127.0.0.1/person/resume_index.asp后面跟着参数
    当时,跳转后,想获取http://127.0.0.1/person/resume_index.asp的html就老是转回登陆页面
      

  5.   

    post.setRequestHeader("Referer", "http://127.0.0.1:8080/login/login.asp"); 
    应该是这样吧!
    愚见!
      

  6.   

    谢谢你的意见。
    因为我设置了80端口,所以http://127.0.0.1/login/login.asp