【问题标题】:Error 302 when trying to post D2L topic using multipart尝试使用 multipart 发布 D2L 主题时出现错误 302
【发布时间】:2013-05-10 17:57:08
【问题描述】:

此问题与one I posted yesterday 有关,但我发现了更多信息,现在遇到了另一个错误。我从 Android 中删除了代码,直接从 Java 中尝试。我还尝试了 2 种不同的方式来获取多帖子信息。 userContext 具有写权限,因为我可以轻松创建模块。

我尝试过的一种方法:

String json = "{\"IsHidden\": false, \"IsLocked\": false, \"ShortTitle\": "Test\",   \"Type\": 1, \r\n" + "\"DueDate\": null, \"Url\": \"file.txt\", \r\n" + "\"StartDate\": null, \"TopicType\": 1, \"EndDate\": null, \"Title\": \"Test topic \r\n" + "content\"} \r\n"; 

URI uri = userContext.createAuthenticatedUri("/d2l/api/le/1.0/Orgid/content/modules/moduleid/structure/", "POST");

MultipartEntity entity = new MultipartEntity();

StringBody part1 = new StringBody(json, "application/json", null);

entity.addPart("json", part1);

File file = new File("test.txt");

FileBody part2 = new FileBody(file, "test.txt", "text/plain", null);

entity.addPart("file", part2);

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(uri);

post.setEntity(entity);

HttpResponse response = httpClient.execute(post);
System.out.println("Statusline: " + response.getStatusLine());

这是我尝试的另一种方式:

String body = "--xxBOUNDARYxx \r\n" +
     "Content-Type: application/json \r\n" +
     "{\"IsHidden\": false, \"IsLocked\": false, \"ShortTitle\": \"Test\", \"Type\": 1, \r\n" +
     "\"DueDate\": null, \"Url\": \"file.txt\", \r\n" +
     "\"StartDate\": null, \"TopicType\": 1, \"EndDate\": null, \"Title\": \"Test topic \r\n" +
     "content\"} \r\n" +
     "--xxBOUNDARYxx \r\n" +
     "Content-Disposition: form-data; name=\"\"; filename=\"file.txt\" \r\n" +
     "Content-Type: text/plain \r\n" +
     " This is a sample text file \r\n" +
     "with some text content. \r\n" +
     "--xxBOUNDARYxx--";

URI uri = userContext.createAuthenticatedUri("/d2l/api/le/1.0/orgid/content/modules/moduleid/structure/", "POST");

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(uri);
post.addHeader("Content-Type", "multipart/mixed;boundary=xxBOUNDARYxx");

post.setEntity(new StringEntity(body));

HttpResponse response = httpClient.execute(post);

System.out.println("Statusline: " + response.getStatusLine());

这两种方法都给出了相同的结果:

Statusline: HTTP/1.1 302 Found
Response: <html><head><title>Object moved</title></head><body><h2>Object moved to <a href="/d2l/error/404">here</a>.</h2></body></html>

恕我直言,这两种技术都应该按照here 的描述创建结构,所以我真的被卡住了。我还尝试将完整的 /content/enforced/.../file.txt 用于 Url,结果相同。

【问题讨论】:

  • 我注意到在第一个 sn-p 中我在一个地方使用了file.txt,在另一个地方使用了test.txt。我固定使用相同的名称,但仍然得到相同的结果。

标签: http rest multipart desire2learn valence


【解决方案1】:

302 重定向实际上是重定向到 404 错误页面。我怀疑,因为您将 302 重定向到系统“404 错误”页面,重定向发生在路由处理层:有可能从堆栈的更深处获得 404,但是你会只需获得对您的请求的直接 404 响应。那么,最可能的原因是 D2L 应用程序堆栈无法将您的 URL(加上一些参数)正确绑定到路由处理程序——也就是说,它无法标记您的请求以了解控制器代码的哪位将请求交给。

我猜你写的时候

userContext.createAuthenticatedUri("/d2l/api/le/1.0/Orgid/content/modules/moduleid/structure/", "POST")

实际上在该路径中输入了真正的Orgidmoduleid 值?因为写入的字符串肯定会返回 404——没有这样的路由——实际上它很可能会完全按照你的描述重定向到系统 404 错误页面。

还要注意,当你写的时候

 ...
 "Content-Type: application/json \r\n" +
 "{\"IsHidden\": false, \"IsLocked\": false, \"ShortTitle\": \"Test\", \"Type\": 1, \r\n" +
 ...

我不确定这是否可行。我相信多部分 HTTP 正文的相关标准表明,您需要用空行标记部分标头与部分数据有效负载的分离,而这里您只是惰性化 one返回。您的数据包将如下所示:

Content-Type: application/json
{"IsHidden": false, "IsLocked": false, ...

当它应该可能看起来像这样:

Content-Type: application/json

{"IsHidden": false, "IsLocked": false, ...

您可以尝试修复这些问题,看看会发生什么。

【讨论】:

  • 太棒了!那条额外的线使一切变得不同,现在它完美地工作了!我欠你一个人情!我还必须在Content-Type: text/plain 之后添加额外的行,否则它会创建一个 0 字节的文件。
  • 是的,接收者必须有某种的方法来从部件标题中辨别部件主体。好的 ol' \r\n 作为分隔符来拯救:太棒了。 8)
猜你喜欢
  • 1970-01-01
  • 2021-02-19
  • 2019-09-05
  • 1970-01-01
  • 2020-05-12
  • 1970-01-01
  • 1970-01-01
  • 2021-03-26
  • 2023-04-05
相关资源
最近更新 更多