我想要獲得id
Cookie,當您選擇登錄ads settings page時,Google會發出一個cookie(如果您已經接受目標廣告,則必須先退出,以查看我所指的頁面)。如何獲取動態Cookie
我發現,爲了得到這個cookie,你必須執行一個HTTP GET
到action
這個URL。問題是這個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
}
我嘗試了以下HtmlUnit: 'WebClient webClient = new WebClient(); HtmlPage page = webClient.getPage(doubleClickURL); DomNodeList forms = page.getElementsByTagName(「form」);' 但它並沒有反映任何形式:S似乎是在超時或什麼後加載。 –
synack