1

下面的代码片段尝试将 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

4

1 回答 1

1

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

于 2013-01-04T15:01:36.057 回答