Java HTTP PUT 与 Java 中的摘要式身份验证

发布于 2024-09-25 05:46:34 字数 1443 浏览 6 评论 0原文

我正在尝试使用 PUT 使用 Java 上传文件,服务器进行摘要式身份验证。我想保持简洁,所以我尝试使用 HttpURLConnection。

public void putData(String path, byte [] data) throws IOException, MalformedURLException {

Authenticator.setDefault(new Authenticator() {
      protected PasswordAuthentication getPasswordAuthentication() {
          return new PasswordAuthentication(user,password.toCharArray());
      }});

  debug("Default authenticator set");
  //Safeguard against double slashes
  if (path.startsWith("/")) {
   path = path.replaceFirst("/","");
  }

  debug(hostname + path);
  URL url = new URL(hostname + path);
  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  debug("HttpURLConnection acquired");
  conn.setDoOutput(true);
        conn.setDoInput(true);

  conn.setRequestMethod("PUT");
  conn.setRequestProperty("Content-Length",String.valueOf(data.length));
  conn.setRequestProperty("Content-type","application/binary");
  conn.setFixedLengthStreamingMode(data.length);
  conn.connect();

  debug("Properties set");
  OutputStream out = conn.getOutputStream();
  debug("Outputstrem acquired");
  out.write(data,0,data.length);
  out.flush();
  out.close();
  debug("Data written, stream closed."); 
 }

由于某种原因,这毫无希望地失败了:我看到 401 返回,然后就完成了。如果我禁用授权,则相同的代码可以工作。使用摘要身份验证下载具有类似代码的文件“正常”。有什么想法吗?我真的不想开始使用下一个那么多的库,比如 Apache 的 htclient 等(…现在是 2010 年了…你希望带有摘要 authN 的 http 请求可以在任何标准库中工作)。

I am trying to upload a file with Java using PUT, server does Digest authentication. I want to keep it lean, so I try to use HttpURLConnection.

public void putData(String path, byte [] data) throws IOException, MalformedURLException {

Authenticator.setDefault(new Authenticator() {
      protected PasswordAuthentication getPasswordAuthentication() {
          return new PasswordAuthentication(user,password.toCharArray());
      }});

  debug("Default authenticator set");
  //Safeguard against double slashes
  if (path.startsWith("/")) {
   path = path.replaceFirst("/","");
  }

  debug(hostname + path);
  URL url = new URL(hostname + path);
  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  debug("HttpURLConnection acquired");
  conn.setDoOutput(true);
        conn.setDoInput(true);

  conn.setRequestMethod("PUT");
  conn.setRequestProperty("Content-Length",String.valueOf(data.length));
  conn.setRequestProperty("Content-type","application/binary");
  conn.setFixedLengthStreamingMode(data.length);
  conn.connect();

  debug("Properties set");
  OutputStream out = conn.getOutputStream();
  debug("Outputstrem acquired");
  out.write(data,0,data.length);
  out.flush();
  out.close();
  debug("Data written, stream closed."); 
 }

For some reason, this fails hopelessly: I see a 401 coming back, and then it's done. If I disable authorization, same code works. Downloading a file with similar code using digest authentication "just works". Any ideas? I'd really rather not start using the next so many libraries like htclient from Apache or so (...it's 2010... you'd expect http requests with digest authN to work in any standard library).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

好听的两个字的网名 2024-10-02 05:46:34

您至少应该在方法末尾尝试 conn.getInputStream() ,以强制评估服务器响应。否则,将无法正确检测来自服务器的潜在错误消息。

You should at least try conn.getInputStream() at the end of your method, to force evaluation of the server response. Otherwise, potential error messages from the server will not be detected properly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文