我从 stackoverflow 的这个示例中逐字获取了 HTTP PUT 代码——我正在尝试将 XML 文件放入 ExistDB REST 接口,但我从 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)什么也不做。
我是否按正确的顺序进行了这些设置调用?有什么方法可以获取有关请求语法为何无效的更多信息?