gpt4 book ai didi

java - 使用 UTF-8 URL 调用 nominatim.openstreetmap OSM

转载 作者:行者123 更新时间:2023-12-01 11:15:53 31 4
gpt4 key购买 nike

我想调用这个URL http://nominatim.openstreetmap.org/search?q= πAπΣι&format=json&polygon=1&addressdetails=1

其中 0ΠAPIΣI 是我从 JSP 获取的参数,

为了确保一切正确,我什至将 URL 写入文件

String urls = "http://nominatim.openstreetmap.org/search?q=" + URLEncoder.encode(addressString.trim(), "UTF-8") + "&format=json&polygon=1&addressdetails=1";
File fileDir = new File("c:\\temp\\testUTF-82.txt");
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileDir), "UTF8"));
out.append(urls);
out.flush();
out.close();

URL url = new URL(urls);

HttpURLConnection uc = (HttpURLConnection) url.openConnection();
uc.setRequestMethod("GET");
uc.setRequestProperty("Accept:", "application/json");
uc.setRequestProperty("Accept-Charset:", "UTF-8");
uc.setRequestProperty("Accept-Charset", "UTF-8");
uc.setRequestProperty("Content-Type:","application/x-www-form-urlencoded;charset=utf-8");
uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");

uc.connect();

但是我遇到了这个奇怪的错误:

Response: '406: Not Acceptable' for url: 'http://nominatim.openstreetmap.org/search?q= %CE%A0%CE%91%CE%A1%CE%99%CE%A3%CE%99&format=json&polygon=1&addressdetails=1'

这是文件 testUTF-82.txt 中的字符串:

http://nominatim.openstreetmap.org/search?q= %CE%A0%CE%91%CE%A1%CE%99%CE%A3%CE%99&format=json&polygon=1&addressdetails=1

最佳答案

链接在浏览器中可以正常打开。它返回一个 JSON 响应,其中包含有效的 OpenStreetMap 输出和 HTTP 200 状态代码。

您的客户端正在接收 HTTP 406 状态代码。 This page给出状态代码的解释。错误的指示原因是服务器上支持的响应格式与客户端上可接受的响应格式之间不匹配。

以下 header 与可接受的响应格式相关:

  • 接受(MIME 类型)
  • Accept-Charset(字符集)
  • 接受编码(文件格式/数据编码)
  • 接受语言(自然语言)
  • Accept-Ranges:(资源的字节范围,即资源的一部分)

您可以尝试显式设置以下请求 header :

“接受:application/json”“接受字符集:UTF-8”

关于java - 使用 UTF-8 URL 调用 nominatim.openstreetmap OSM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31825347/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com