gpt4 book ai didi

java - 使用 Java 和 Apache HttpClient 将附件发布到 confluence 时出现 404

转载 作者:行者123 更新时间:2023-12-02 03:20:12 24 4
gpt4 key购买 nike

我正在尝试将图像作为内容的附件发布到融合页面。

这是我的java应用程序的功能:

public void postAttachment(File f, String comment) throws IOException {

if (!f.exists() || !f.canRead()) {
throw new IOException("Could not access file " + f.getAbsolutePath());
}

final FileBody bin = new FileBody(f);
final StringBody cmt = new StringBody(comment, ContentType.TEXT_PLAIN);
final HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("file", bin).addPart("comment", cmt).build();

final HttpPost post = new HttpPost(baseUrl + "/rest/api/content/" + contentid + "/child/attachment");
post.setEntity(reqEntity);

post.addHeader(BasicScheme.authenticate(new UsernamePasswordCredentials(props.getString("confluence_user"),props.getString("confluence_password")), "UTF-8", false));
post.addHeader("X-Atlassian-Token:","no-check");

System.out.println("executing request " + post.getRequestLine());
final CloseableHttpClient httpclient = HttpClients.createDefault();


final CloseableHttpResponse response = httpclient.execute(post);

System.out.println(post.getRequestLine());
System.out.println(response.toString());

if (response.getStatusLine().getStatusCode() == 404) {
throw new IOException("Status 404 thrown!");
}

}

终端的输出是:

POST https://xxx.xxxx.de:443/rest/api/content/38262140/child/attachment 

然后

HttpResponseProxy{HTTP/1.1 404 Not Found [Server: Apache-Coyote/1.1, X-ASEN: SEN-1343236, Set-Cookie: JSESSIONID=9DF46011711C2828977E17A945D023E1; Path=/; Secure; HttpOnly, X-Seraph-LoginReason: OK, X-AUSERNAME: xxxx, X-Content-Type-Options: nosniff, Content-Type: text/plain, Transfer-Encoding: chunked, Date: Tue, 27 Sep 2016 11:20:35 GMT] ResponseEntityProxy{[Content-Type: text/plain,Chunked: true]}}

(我只是为了这篇文章更改了域名和用户名..)

看来一切都还好。如果我复制生成的 POST url 并在浏览器中执行 GET,我会得到一个带有附件列表的 json 片段,之前手动上传。所以 POST url 应该没问题。

我在网上进行了搜索,但找不到我的代码哪里出错了。有什么建议吗?

最佳答案

Confluence 对“X-Atlassian-Token” header 值非常严格。 header 名称中有额外的 :

改变

 post.addHeader("X-Atlassian-Token:","no-check");

 post.addHeader("X-Atlassian-Token","no-check");

这将创建正确的 header ,并且 404 错误将消失

关于java - 使用 Java 和 Apache HttpClient 将附件发布到 confluence 时出现 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39723708/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com