この例の HTTP PUT コードを、stackoverflowからほぼそのまま引用しました。XML ファイルを ExistDB レスト インターフェイスに配置しようとしていますが、URLConnection getResponseCode() から「400 Bad Request」値を受け取り、何も記録されていません。リモート サーバーによって、要求が送信されなかったかのようになります。
URL target = new URL( "http://domain.com:8080/exist/rest/db/collection/doc.xml" )
HttpURLConnection uc = (HttpURLConnection) target.openConnection();
uc.setDoOutput(true);
uc.setDoInput(true);
uc.setRequestMethod("PUT");
uc.setConnectTimeout(CONNECT_TIMEOUT); // timeouts are both 2000L
uc.setReadTimeout(READ_TIMEOUT); //ms
uc.setRequestProperty("Content-type", "text/xml");
// basic Auth s.getLogin() already recorded as username:pwd format
String auth = new String( Base64.encodeBase64String( s.getLogin().getBytes() ));
auth = auth.replaceAll("\n",""); // sun bug_id 6459815
uc.setRequestProperty("Authorization", "Basic " + auth );
uc.connect(); // tried moving this to after the body section below
PrintWriter body = new PrintWriter(uc.getOutputStream());
InputStream sml = smlStream(this.pathid);
StringBuilder xml = new StringBuilder( new Scanner(sml).useDelimiter("\\A").next());
body.print(xml);
body.close();
uc.disconnect(); // appears to make no difference
uc.getResponseCode() の値が 400 になりました
本文の同じ URL と同じ xml ファイルで curl を使用する場合 (curl -v -u uname:pw -T file.xml http://domain.com:8080/exist/rest/db/collection/doc.xml )同じマシンから同じユーザー IDで要求を送信すると、ドキュメントは通過し、期待どおりに 201 応答を返します。一方、Java バージョン (openjdk7 を使用) は何もしません。
これらのセット呼び出しは正しい順序で行われていますか? リクエスト構文が無効である理由について、より多くの情報を取得する方法はありますか?