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");

置換後、URL は次のようになります。

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

テストするには、URL をブラウザに直接入力してみてください。

于 2015-02-27T13:53:08.047 に答える