我想获取id
您在广告设置页面上选择加入时 Google 发出的 cookie (如果您已经接受目标广告,则必须先选择退出才能看到我所指的页面)。
我发现,为了获取此 cookie,您必须以该页面中的形式GET
对URL 执行 HTTP。action
问题是这个 URL 包含一个散列,它会随着每个新的 HTTP 连接而改变,所以,首先,我必须去这个页面并获取这个 URL,然后,GET
对 URL 执行。
我正在使用 HttpComponents 来获取http://www.google.com/ads/preferences但是当我使用 JSOUP 解析内容时,只有一个脚本并且找不到任何表单。
恐怕会发生这种情况,因为内容是使用某种超时动态加载的……有谁知道解决方法吗?
编辑:顺便说一句,我现在使用的代码是:
HttpClient httpclient = new DefaultHttpClient();
// Create a local instance of cookie store
CookieStore cookieStore = new BasicCookieStore();
// Bind custom cookie store to the local context
((AbstractHttpClient) httpclient).setCookieStore(cookieStore);
CookieSpecFactory csf = new CookieSpecFactory() {
public CookieSpec newInstance(HttpParams params) {
return new BrowserCompatSpec() {
@Override
public void validate(Cookie cookie, CookieOrigin origin)
throws MalformedCookieException {
// Allow all cookies
System.out.println("Allowed cookie: " + cookie.getName() + " "
+ cookie.getValue() + " " + cookie.getPath());
}
};
}
};
((AbstractHttpClient) httpclient).getCookieSpecs().register("EASY", csf);
// Create local HTTP context
HttpContext localContext = new BasicHttpContext();
// Bind custom cookie store to the local context
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
HttpGet httpget = new HttpGet(doubleClickURL);
// Override the default policy for this request
httpclient.getParams().setParameter(
ClientPNames.COOKIE_POLICY, "EASY");
// Pass local context as a parameter
HttpResponse response = httpclient.execute(httpget, localContext);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(instream));
instream.close();
// Find action attribute of form
Document document = Jsoup.parse(reader.readLine());
Element form = document.select("form").first();
String optinURL = form.attr("action");
URL connection = new URL(optinURL);
// ... get id Cookie
}