gpt4 book ai didi

java - 将从一个响应中获取的 sessionId 传递给下一个请求

转载 作者:行者123 更新时间:2023-12-01 05:27:50 29 4
gpt4 key购买 nike

我需要以编程方式从 Google Insights 下载 CSV 文件。由于它需要身份验证,我使用 clientLogin 来获取 session id。

如何通过将 session ID 作为 cookie 传递来下载文件?

我尝试使用新的 URLConnection 对象并在 setRequestParameter 方法中设置 cookie,希望它能够验证我的登录身份,但它似乎不起作用。我有一种感觉,我不应该使用两个单独的连接,是这样吗?

如果是这样,那么当我下载文件时如何传递 session ID 作为参数?我也尝试使用相同的连接,但这也不起作用。请帮忙。

try {
URL url1 = new URL("https://www.google.com/accounts/ClientLogin?accountType=GOOGLE&Email=*******.com&Passwd=*****&service=trendspro&source=test-test-v1");
URL url2 = new URL("http://www.google.com/insights/search/overviewReport?cat=0-7&geo=BR&cmpt=geo&content=1&export=1");
URLConnection conn = url1.openConnection();

// fake request coming from browser
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11");
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String f = in.readLine();
// obtaining the sid.
String sid=f.substring(4);
System.out.println(sid);

URLConnection conn2 = url2.openConnection();
conn2.setRequestProperty("Cookie", sid);
BufferedInputStream i= new BufferedInputStream(conn2.getInputStream());
FileOutputStream fos = new FileOutputStream("f:/testplans.csv");
BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
byte data[] = new byte[1024];

while(i.read(data,0,1024)>=0) {
bout.write(data);
}
bout.close();
in.close();
}

最佳答案

尝试以下操作:link 。检查最上面的答案:他们不使用 SID,而是使用 Auth。

如果它适用于 Google Reader,那么它也可能适用于 Google Insights。

关于java - 将从一个响应中获取的 sessionId 传递给下一个请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9507690/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com