我想使用谷歌的語言檢測API,現在我使用谷歌文檔中提供的示例如下:谷歌語言檢測API回答錯誤代碼406
public static String googleLangDetection(String str) throws IOException, JSONException{
String urlStr = "http://ajax.googleapis.com/ajax/services/language/detect?v=1.0&q=";
// String urlStr = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=Paris%20Hilton";
URL url = new URL(urlStr+str);
URLConnection connection = url.openConnection();
// connection.addRequestProperty("Referer","http://www.hpeprint.com");
String line;
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while((line = reader.readLine()) != null) {
builder.append(line);
}
JSONObject json = new JSONObject(builder.toString());
for (Iterator iterator = json.keys(); iterator.hasNext();) {
String type = (String) iterator.next();
System.out.println(type);
}
return json.getString("language");
}
但我得到的HTTP錯誤代碼「 406' 。
我無法理解問題是什麼?作爲谷歌搜索查詢(評論)下面它工作正常。
結果語言檢測網址本身工作正常,當我在Firefox或IE中運行它,但它在我的Java代碼中失敗。
有什麼我做錯了嗎?
在此先感謝
阿希什
嘗試使用調試代理來看看該請求例如http:/ /www.fiddler2.com/(運行代理時用'-Dhttp.proxyhost = localhost -Dhttp.proxyport = 8888'運行程序)。 – axtavt 2010-09-27 15:34:21