我有一个java代码,使用百度地图地理代码API检索一个地址的经度/纬度信息。问题是答案(具有XML/JSon格式的inputStream )总是相同的:状态=1和msg =内部服务错误。
起初,我认为它可能是我的代码创建的一个糟糕的url,但是在任何浏览器中,这个url都可以正常工作。
我添加了我的代码,这样您就可以看到它是否有问题,我没有注意到:
private static final String URLDIR = "http://api.map.baidu.com";
private static final String URLPATH = "/geocoder/v2/?";
public void getGeocoding(String address)
throws IOException
{
String HostName = AutoCompletar.getProxyName();
int HostPort = AutoCompletar.getProxyPort();
String uri = "";
String uriEncode = "";
this.latitude = "";
this.longitude = "";
this.estado = "";
System.out.print("\nCalculate lat,lng: " + address + "\n");
System.out.print("-----------------------------------------------------\n");
try {
uri = URLDIR + URLPATH;
String uriQuery = "address=" + address;
uriQuery = uriQuery + "&output=xml" + "&ak=" + baiduKey;
System.out.println("uriQuery: " + uriQuery);
uriEncode = uri + uriQuery;
System.getProperties().put("http.proxyHost", HostName);
System.getProperties().put("http.proxyPort", Integer.toString(HostPort));
URL bmap = new URL(uriEncode);
HttpURLConnection yc = null;
if (HostName == null || HostName.equals("")) {
yc = (HttpURLConnection) bmap.openConnection();
} else {
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(HostName,HostPort));
yc = (HttpURLConnection) bmap.openConnection(proxy);
}
//yc.connect();
BufferedReader in =
new BufferedReader(new InputStreamReader(yc.getInputStream()));
InputSource inputXml = new InputSource(in);
try {
SimpleParser xmlParser = new SimpleParser(inputXml);
System.out.println(xmlParser.toString());
this.estado = xmlParser.nodeValue("/GeocoderSearchResponse/status/text()");
System.out.print("Status: " + this.estado + "\n");
if ("0".equals(this.estado)) {
this.latitude = xmlParser.nodeValue("/GeocoderSearchResponse/result/location/lat/text()");
this.longitude = xmlParser.nodeValue("/GeocoderSearchResponse/result/location/lng/text()");
System.out.print("lat,lng: " + this.latitude + "," + this.longitude + "\n");
} else {
throw new IOException(this.estado);
}
}
catch (ParserConfigurationException ex) {
ex.printStackTrace();
} catch (SAXException ex) {
ex.printStackTrace();
} catch (XPathExpressionException ex) {
ex.printStackTrace();
} catch (IOException ex) {
System.out.println("Status :" + ex.getMessage());
dispatchMessage(ABORTED);
}
in.close();
}
catch (UnsupportedEncodingException Ur) {
System.out.println("Error UnsupportedEncodingException. " + Ur.getMessage());
System.out.println(uri);
System.out.println("URI Encode: " + uriEncode);
dispatchMessage(ABORTED);
}
catch (IOException Ur) {
System.out.println("Error geocoding from Google. " + Ur);
dispatchMessage(ABORTED);
}
}先谢谢你。
发布于 2014-12-03 15:19:44
我不确定,但我认为这是因为您的请求没有User-Agent头,有些服务器有此策略,以便不将数据作为应用程序发送给机器人。
就像这样
System.setProperty("http.agent", "<<a_user_aganet/such_as_chrome>>");//generally
//OR
yc.setRequestProperty("User-Agent", "<<a_user_aganet/such_as_chrome>>");首先,最好联系并查找从目标服务器获取信息的API(btb)方法。
您可能会用一些假的用户代理来愚弄the服务器,但是如果目标服务器设计得非常严格,经过一些串行请求之后,它可能会通过captcha向您发起挑战。
https://stackoverflow.com/questions/27270121
复制相似问题