gpt4 book ai didi

java - 连接到URL失败,但是当我直接在浏览器中尝试时,它成功了

转载 作者:太空宇宙 更新时间:2023-11-04 07:52:49 27 4
gpt4 key购买 nike

以下代码片段尝试将 2 个参数连同 URL 一起发送到 servlet。但是当我尝试这个片段时,我收到一条消息:

Connection to file server failed

但是如果我直接尝试该 URL:

http://localhost:8084/nappster/ReceiveFileName?fileName=" + fileName + "&ip=" + IP

有了数据,没有问题。 Servlet 接收文件名并按预期进行处理。当我尝试通过代码连接到 URL 时,失败并在浏览器中尝试时成功,这可能是什么原因。

  protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String fileName = request.getParameter("FileTag");
String IP = new ClientAddress().getNetworkIP();
// Send the file name to the nappster server
URL url = new URL("http://localhost:8084/nappster/ReceiveFileName?fileName=" + fileName + "&ip=" + IP);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
if(connection.getResponseCode() == 200) {
// File name sent successfully to the server
System.out.println("Connection to file server successful");
System.out.println("--------");
} else {
// Unable to send file name to the server
System.out.println("Connection to file server failed");

}
}

注意:

当我尝试上面的代码片段时返回的响应代码是505

最佳答案

尝试使用url.encode()方法。

关于java - 连接到URL失败,但是当我直接在浏览器中尝试时,它成功了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14159341/

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