如题?
要抓取需要登陆的页面怎么办啊

解决方案 »

  1.   

    package test;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;import javax.servlet.http.HttpServletResponse;import org.apache.commons.httpclient.*;
    import org.apache.commons.httpclient.cookie.*;
    import org.apache.commons.httpclient.methods.*;
    public class FormLoginDemo {
        static final String LOGON_SITE = "www.alibaba.com.cn";
        static final int    LOGON_PORT = 80;
        public static void main(String[] args) throws Exception{

         Map<String, String> headers = new HashMap<String, String>();
        
         HttpClient client = new HttpClient();
            client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT);        PostMethod method = new PostMethod("/member/signin.htm");
            NameValuePair name = new NameValuePair("LoginId", "vp25");     
            NameValuePair pass = new NameValuePair("Password", "12121212");     
            method.setRequestBody(new NameValuePair[]{name,pass});
        
            client.executeMethod(method);        System.out.println(method.getResponseBodyAsString());
        
        for (Header header : method.getResponseHeaders("Set-Cookie")) {
            String[] sArray = header.getValue().split(";")[0].split("=");
            System.out.println(sArray[1]);
            headers.put(sArray[0], sArray[1]);
          }
        StringBuffer buffer = new StringBuffer();
        for (Iterator iterator = headers.keySet().iterator(); iterator
            .hasNext();) {
          String key = (String) iterator.next();
          buffer.append(buffer.length() > 0 ? "; " : "").append(key).append("=").append(headers.get(key));
          System.out.println(key);
        }
        
        GetMethod get = new GetMethod("http://china.alibaba.com/member/myalibaba.htm?ca=true");
        get.addRequestHeader("Set-Cookie", buffer.toString());
        client.getParams().setParameter("http.protocol.content-charset","GBK");
        if (client.executeMethod(get) == HttpStatus.SC_OK) {
         System.err.println(get.getResponseBodyAsString());
     
        }
      }
    }
    这是我写的一个代码,但是调试的时候出现问题,不知道是什么问题,高手帮忙看下阿
      

  2.   

    1、
    method.setRequestBody(new NameValuePair[]{name,pass});
    PostMethod 一般用的是 addXXX(NameValuePair[])2、
    client.executeMethod(method);
    调试的时候应该把这个方法返回的status打印出来以便判断
    再有其它的我就不懂不敢说了,当然还觉得楼主这段代码为何而写还没看出来