最近需要做一个抓取的功能
http://17track.net/Index.htmlRB373021971HK里边的查询是通过ajax查询的
使用浏览器访问http://17track.net/Index.html的时候,使用httpwatch看不到是何时获取到cookie的,只看到一会就sent cookie了,没有看到received cookie.不知道有没有人遇到这样的事情?
使用httpclient访问http://17track.net/Index.html这个地址也是没有获取到cookie.
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
public class XCD { /**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
HttpClient client = new HttpClient();
List<Header> headers = new ArrayList<Header>();  
headers.add(new Header("Host","17track.net"));
headers.add(new Header("User-Agent","Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; CIBA; .NET4.0C; .NET CLR 2.0.50727)"));
headers.add(new Header("Accept","image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/QVOD, application/QVOD, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/xaml+xml, application/x-ms-xbap, application/x-ms-application, */*"));
headers.add(new Header("Accept-Language","zh-cn"));
headers.add(new Header("Accept-Encoding","gzip, deflate,utf-8"));
headers.add(new Header("Accept-Charset","GB2312,utf-8;q=0.7,*;q=0.7"));
headers.add(new Header("Connection","Keep-Alive"));

client.getHostConfiguration().getParams().setParameter("http.default-headers", headers);    GetMethod method2 = new GetMethod("http://17track.net/Index.html");
method2.getParams().setCookiePolicy(CookiePolicy.DEFAULT); 
try {
System.out.println(client.executeMethod(method2));
//System.out.println(method2.getResponseBodyAsString());
System.out.println(client.getState().getCookies().length);
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}
asp.net的大牛们,谁有空帮我分析分析啊.为什么取不到cookie?而使用浏览器却又可以得到cookie?

解决方案 »

  1.   

    HttpClient httpClient = new DefaultHttpClient();   
            HttpClientParams.setCookiePolicy(httpClient.getParams(), CookiePolicy.BROWSER_COMPATIBILITY);     
            HttpHost httpHost = new HttpHost("localhost");   
            HttpGet httpGet = new HttpGet("/https/");   
               
            HttpResponse response = httpClient.execute(httpHost,httpGet);   
               
            if(HttpStatus.SC_OK==response.getStatusLine().getStatusCode()){   
                //请求成功   
                //取得请求内容   
                HttpEntity entity = response.getEntity();   
                //显示内容   
                if (entity != null) {   
                    // 显示结果   
                    System.out.println(EntityUtils.toString(entity,"utf-8"));   
                }   
            }   
            //模拟写cookie   
            httpGet = new HttpGet("/https/index.jsp?cookie=write");   
            response = httpClient.execute(httpHost,httpGet);   
            FileWriter fw = new FileWriter("C:/cookie.txt");    
            //读取cookie并保存文件   
            List<Cookie> cookies = ((AbstractHttpClient) httpClient).getCookieStore().getCookies();     
            if (cookies.isEmpty()) {     
                System.out.println("None");     
            } else {     
                for (int i = 0; i < cookies.size(); i++) {   
                    System.out.println("- " + cookies.get(i).toString());   
                    fw.write(cookies.get(i).toString()+"\r\n");    
                }     
            }   
            fw.close();   
              
            if(HttpStatus.SC_OK==response.getStatusLine().getStatusCode()){   
                //请求成功   
                //取得请求内容   
                HttpEntity entity = response.getEntity();   
                //显示内容   
                if (entity != null) {   
                    // 显示结果   
                    System.out.println(EntityUtils.toString(entity,"utf-8"));   
                }   
            }
      

  2.   

    楼上的大虾~~可不可以帮我按题目里的URL帮我写一下啊?