2

bitbucket の問題 API アクセス用の Java ライブラリを開発したいと考えています。HTTP Content-Length ヘッダーの計算
については既に質問しましたが、この質問は特にBitbucket APIと問題を更新するプロセスに関するものです (他のすべての要求が適切に機能するため)。

次のコードは機能せず、411 Length Requiredエラーが発生します。
しかし、さらにややこしい: ドキュメントでは、PUTリクエスト メソッドを使用するように指示されています。それを指定するのを「忘れた」場合、ステータス コードは に変わりますが200 OK、問題は変更されません。

public class PutTest {
    public static void main(String[] args) throws Exception {
        URL u = new URL("https://api.bitbucket.org/1.0/repositories/myname/myproject/issues/1/?title=hello+world");
        HttpURLConnection c = (HttpURLConnection) u.openConnection();
        c.addRequestProperty("Authorization", "Basic "+Base64.encodeToString("user:password".getBytes(), false));
        c.addRequestProperty("Content-Length", String.valueOf(u.getQuery().getBytes("UTF-8").length));
        c.setRequestMethod("PUT");
        c.connect();
        System.out.println(c.getResponseCode()+" "+c.getResponseMessage());
    }
}
4

1 に答える 1

1

私の更新されたコード サンプルは、stackoverflow の別の質問の助けを借りて動作します: How to send PUT, DELETE HTTP request in HttpURLConnection? 動作していないようです。

OutputStreamコネクションの作品を活用。

public class PutTest {

    public static void main(String[] args) throws Exception {
        URL u = new URL("https://api.bitbucket.org/1.0/repositories/myname/myproject/issues/1/");
        HttpURLConnection c = (HttpURLConnection) u.openConnection();
        c.addRequestProperty("Authorization", "Basic "+Base64.encodeToString(("user:password").getBytes(), false));
        c.setRequestMethod("PUT");
        c.setDoOutput(true);
        OutputStreamWriter out = new OutputStreamWriter(c.getOutputStream());
        out.write("title=hello+world");
        out.close();
        c.connect();
        System.out.println(c.getResponseCode()+" "+c.getResponseMessage());
    }
}
于 2011-08-02T12:32:38.587 に答える