【发布时间】:2012-10-28 20:42:11
【问题描述】:
我是这方面的新手...基本上我需要运行一个脚本来从谷歌趋势下载 .csv 文件。我按照这个reference写了如下代码,代码是这样的:
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("https://www.google.com/accounts/ClientLogin");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>;
nameValuePairs.add(new BasicNameValuePair("Email", "myEmail"));
nameValuePairs
.add(new BasicNameValuePair("Passwd", "myPasswd"));
nameValuePairs.add(new BasicNameValuePair("accountType", "GOOGLE"));
nameValuePairs.add(new BasicNameValuePair("source",
"Google-cURL-Example"));
nameValuePairs.add(new BasicNameValuePair("service", "xapi"));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(post);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
System.out.println(line);
if (line.startsWith("SID=")) {
String key = line.substring(4);
// Do something with the key
} catch (Exception e) {
}
我得到了有关 SID、LSID、Auth 的信息,但不知道如何使用这些信息。我想我应该在我的以下请求中添加这些 cookie,但不知道具体如何。我编写了另一段代码来连接到某个 URL,但我不断收到这条消息“您必须登录才能从 Google 趋势中导出数据”。如果有帮助,代码就在这里:
URL url = new URL(myUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setInstanceFollowRedirects(true);
conn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.addRequestProperty("Authorization", "SID"+key);
conn.addRequestProperty("Email", "myEmail");
conn.addRequestProperty("Passwd", "myPasswd");
conn.setReadTimeout(5000);
conn.connect();
我四处搜索,发现很少有用的信息,有人可以帮忙吗?
【问题讨论】:
标签: httpclient apache-httpclient-4.x google-api-java-client