我正在尝试使用以下代码获取页面内容(android):
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/536.28.10 (KHTML, like Gecko) Version/6.0.3 Safari/536.28.10");
try {
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
String s = "";
while ((s = buffer.readLine()) != null) {
builder.append(s);
}
} catch (Exception e) {
e.printStackTrace();
}
这适用于Google等网站,但我想访问的特定网站(http://www.swimrankings.net/index.php?internalRequest=athleteLookup&athlete_lastname=jansen&athlete_clubId=273&athlete_gender=-1)会返回PHP错误(Fatal error: Access to undeclared static property: Config::$defaultLanguage in C:\Data\htdocs\www.swimrankings.net\classes\BSLanguage.php on line 16
)。
使用浏览器查看网站时,我不会收到此错误消息。我能做些什么来解决这个问题吗?
谢谢!
注意 我已经尝试过使用内置的java URLConnection。我也尝试将标题设置为与safari显示我用于访问页面完全相同,但无济于事。
答案 0 :(得分:1)
感谢@mthmulders的修复!问题是未定义 Accept-Language ,导致PHP出错。将其设置为语言(" en"," de"," fr")解决了问题。
固定代码:
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/536.28.10 (KHTML, like Gecko) Version/6.0.3 Safari/536.28.10");
httpGet.setHeader("Accept-Language", "en");
try {
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
String s = "";
while ((s = buffer.readLine()) != null) {
builder.append(s);
}
} catch (Exception e) {
e.printStackTrace();
}