3

以下代码段尝试连接到 url。该 url 还发送一个名为FileNames. 但是当我运行这个片段时,我总是得到HTTP Version Not Supported回复。可能是什么原因?

try {
  URL url = new URL(AddressInformation.clientAddressToSendFileNames + URLEncoder.encode(list.toString(), "UTF-8")); // STATEMENT-1
  HttpURLConnection connection = (HttpURLConnection) url.openConnection();

  if(connection.getResponseCode() == 200) {
    // File names sent to the client that requested it
    System.out.println("File names sent");
  } else {
     // Error : While trying to send the file names
     System.out.println("Unable to send file names");
     System.out.println("RESPONSE CODE ------->>>>" + connection.getResponseMessage());
   }
} catch(Exception exc) {
   exc.printStackTrace();
  }

在 STATEMENT-1 中,AddressInformation.clientAddressToSendFileNames对应于

http://localhost:8084/D Nappster/ReceiveFileNames?FileNames=.

我正在运行 Tomcat 7。

4

2 回答 2

4

您的 URL 似乎无效(其中包含一个空格),但 URL 构造函数没有检测到这一点。

相反,您可以使用URI将为您正确编码事物的构造函数,而无需担心,然后将其转换为 URL:

URI uri = new URI("http", null, "thehost", theport, "thepath", "thequery", null);
URL url = uri.toURL();

当然,这需要更改您的AddressInformation.

有关更多详细信息,请参阅Javadoc

于 2013-01-12T09:56:35.743 回答
1

好吧,就我而言,我解决了这个问题,在 httpConnection.getResponseCode() 语句之前在我的 url 中进行了替换:用 %20 替换空格。

前任:

String url = "http://localhost:8080/Service/methodName?imagem=MY IMAGE NAME";
url = url.replace(" ", "%20");

替换后,网址将如下所示:

http://localhost:8080/Service/methodName?imagem=MY%20IMAGE%20NAME

为了测试它,尝试将 url 直接放入浏览器中。

于 2015-02-27T13:53:08.047 回答