我正在尝试在bibucket创建新问题,但我不知道如何使用http。我尝试了很多东西,但它仍然没有用。这是我的一次尝试:
URL url = new URL("https://api.bitbucket.org/1.0/repositories/"
+ accountname + "/" + repo_slug + "/issues/"
+ "?title=test&content=testtest");
HttpsURLConnection request = (HttpsURLConnection) url.openConnection();
request.setRequestMethod("POST");
consumer.sign(request);
request.connect();
我对GET请求没有问题。但在这里我不知道如何发送参数并签署消息。
以下是API的文档 https://confluence.atlassian.com/display/BITBUCKET/issues+Resource#issuesResource-POSTanewissue
如何正确地做到这一点?
答案 0 :(得分:2)
最后我想出来了。参数不是URL的一部分,但如果使用流,则无法对其进行签名。
解决方案是使用Apache HttpComponents库并添加如下代码中的参数:
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("https://api.bitbucket.org/1.0/repositories/"
+ accountname + "/" + repo_slug + "/issues/");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("title", "test"));
nvps.add(new BasicNameValuePair("content", "testtest"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
consumer.sign(httpPost);
HttpResponse response2 = httpclient.execute(httpPost);
try {
System.out.println(response2.getStatusLine());
HttpEntity entity2 = response2.getEntity();
// do something useful with the response body
// and ensure it is fully consumed
EntityUtils.consume(entity2);
} finally {
httpPost.releaseConnection();
}
}
但你必须使用CommonsHttpOAuthConsumer,它位于commonshttp的特殊路标库中。
答案 1 :(得分:1)