gpt4 book ai didi

java - 为什么 Http Post 在发送到 ip 时有效,但在发送到 url 时无效?

转载 作者:行者123 更新时间:2023-12-02 06:21:16 26 4
gpt4 key购买 nike

我正在使用org.apache.http.client.methods.HttpPost从我的服务器获取一行信息。

当我使用服务器的 IP 地址时(以下仅是示例),我得到了预期的信息:

HttpPost("http://127.0.0.1/api/fetchInfo")

但是,当我使用 URL 时,服务器未收到请求,并且客户端收到运行时错误:org.apache.http.client.HttpResponseException:未找到。

HttpPost("http://subdomain.domain.com/api/fetchInfo")

URL 包含子域。

我尝试访问的服务器是网站提供商,我可以通过域与网站交互,因此域存在。

客户端在我的本地计算机上,服务器在亚马逊ec2上。

如何让它与 url 一起工作?

我的代码:

    String responseString = "";
try {

MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
multipartEntityBuilder.setCharset(Charset.forName("UTF-8")).setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

HttpEntity entity = multipartEntityBuilder.build();
HttpPost request = new HttpPost("http://subdomain.domain.com/api/fetchInfo");

request.setEntity(entity);

HttpClient client = HttpClientBuilder.create().build();
HttpResponse response = client.execute(request);

responseString = new BasicResponseHandler().handleResponse(response);
} catch (IOException ex) {
log(ex)
}
return responseString;

最佳答案

如果您使用的是 apache 服务器,则需要使用服务器名称 subdomain.domain.com 配置 subdomain.conf 文件,并在计算机上的/etc/hosts 文件中指定 subdomain.domain 的映射.com 名称,IP 127.0.0.1。

我希望这会有所帮助。

关于java - 为什么 Http Post 在发送到 ip 时有效,但在发送到 url 时无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55831368/

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