来自OpenStreetMap的 Nominatim响应不再可读
我正在获取GPS数据的反向查询(发出“纬度”和“经度”)以从nominatim/OpenStreetMap得到所提供位置的地址。
例如:我的GPS坐标是lat=50.59865和 lon=8.41693。
通过发出:
https://nominatim.openstreetmap.org/reverse?lat=50.59865&lon=8.41693&zoom=20&format=json&accept-language=de_DE&addressdetails=1
我期望得到以下JSON作为响应:
{"place_id":127743226,"licence":"Data © OpenStreetMap contributors, ODbL 1.0. http://osm.org/copyright","osm_type":"way","osm_id":921342942,"lat":"50.5986804","lon":"8.4167133","class":"building","type":"yes","place_rank":30,"importance":5.143434805824395e-05,"addresstype":"building","name":"","display_name":"41, Bachstraße, Werdorf, Aßlar, Lahn-Dill-Kreis, Hessen, 35614, Deutschland","address":{"house_number":"41","road":"Bachstraße","village":"Werdorf","municipality":"Aßlar","county":"Lahn-Dill-Kreis","state":"Hessen","ISO3166-2-lvl4":"DE-HE","postcode":"35614","country":"Deutschland","country_code":"de"},"boundingbox":["50.5986344","50.5987271","8.4166294","8.4167991"]}
这在浏览器(Chrome / Firefox)中工作正常,直到去年在Java/JSoup也曾正常工作。
Response response=Jsoup
.connect(url)
.timeout(20000)
.header("Content-Type","application/json; charset=utf-8")
.header("Accept-Encoding","gzip,deflate,br")
.method(Method.GET)
.maxBodySize(0)
.ignoreContentType(true)
.execute();
但现在我收到了一个“Status=403 / Forbidden”。
我把这个请求头与浏览器的请求头进行了对比,并在请求中添加了 userAgent(DEFAULT_UA)
这是现在的新请求:
Response response=Jsoup
.connect(url)
.timeout(20000)
.header("Content-Type","application/json; charset=utf-8")
.header("Accept-Encoding","gzip,deflate,br")
.userAgent(DEFAULT_UA)
.method(Method.GET)
.maxBodySize(0)
.ignoreContentType(true)
.execute();
响应看起来是这样的。我不知道编码是什么。

非常欢迎任何关于原因以及如何处理的建议!
解决方案
使用自定义的User-Agent,而不是浏览器默认的UA:
Response response = Jsoup
.connect(url)
.timeout(20000)
.header("Accept-Encoding", "gzip,deflate,br")
.userAgent("MyApp/1.0 ([email protected])")
.method(Method.GET)
.maxBodySize(0)
.ignoreContentType(true)
.execute();
Nominatim的使用策略要求客户端具备身份标识;这就是他们屏蔽默认浏览器UA,以及空/通用UA的原因。
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。