gpt4 book ai didi

api - 使用 HTTPURLConnection 的 Docker 容器之间的 400 错误 HTTP GET 请求

转载 作者:行者123 更新时间:2023-11-28 22:39:29 25 4
gpt4 key购买 nike

我在 docker-compose 文件中定义了两个容器:

 
tomcat_webserver_api:
image: tomcat:8
volumes:
- ./API/Docker/API.war:/usr/local/tomcat/webapps/API.war
ports:
- "8080:8080"
depends_on:
- mysql_database

tomcat_webserver_anwendung:
image: tomcat:8
ports:
- "8081:8080"
volumes:
- ./Anwendung/Docker/Anwendung.war:/usr/local/tomcat/webapps/Anwendung.war
depends_on:
- tomcat_webserver_api
environment:
API_HOST: tomcat_webserver_api
API_PORT: 8080

现在我想使用 HttpURLConnection 从 Java Web 应用程序内部访问 URL http://tomcat_webserver_api:8080/API/restaurants/Wochentag

问题:它返回 400 错误

java.io.IOException: Server returned HTTP response code: 400 for URL: http://tomcat_webserver_api:8080/API/restaurants/Wochentag

代码就是这样(当我尝试通过 curl 连接到 URL 时, header 几乎相同 - 这在容器内有效,嗯):


URL api = UriBuilder.fromUri("http://" + "tomcat_webserver_api" + ":" + "8080" +"/API/restaurants/RestaurantSpeisen").build().toURL();

System.setProperty("http.agent", "curl/7.52.1");

HttpURLConnection connection = (HttpURLConnection) api.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Host", "localhost");
connection.setRequestProperty("User-Agent", "curl/7.52.1");
connection.connect();

BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));

如果我尝试连接到 http://172.20.0.3:8080/API/restaurants/Wocentag,我会得到一个 200-ok HTTP 响应代码和 JSON 数据。

如果我执行 API 容器并检查日志,我可以看到 400 GET 请求。

为什么会这样?

http://172.20.0.3:8080/API/restaurants/Wocentag - 有效http://tomcat_webserver_api:8080/API/restaurants/Wochentag - 无法工作但不会出现 404 错误

最佳答案

我遇到了和你一样的问题,显然下划线不允许作为虚拟主机,尝试将其删除,例如,只使用 tomcatwebserverapi,这应该可以解决你的问题。

参见 Can (domain name) subdomains have an underscore "_" in it?有关主机名中有效字母的更多信息。

关于api - 使用 HTTPURLConnection 的 Docker 容器之间的 400 错误 HTTP GET 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51077285/

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