【发布时间】:2016-06-14 07:14:03
【问题描述】:
从 cUrl 迁移到 Android 有几个问题,但我发现没有一个涉及文件上传。
此 CUrl 请求有效:
$ curl -X PUT -u user:password --data-binary @myfile.pdf "https://example.com/myurl?id=myid&filename=myfile.pdf&title=My+Title&mimetype=application/pdf"
我正在尝试:
String url = "https://example.com/myurl?id=myid&filename=myfile.pdf&title=My+Title&mimetype=application/pdf";
String fileUrl = "myfile.pdf";
String userPassword = "user:password";
String userpass = Base64.encodeToString(userPassword.getBytes(), Base64.DEFAULT);
File f = new File(fileUrl);
if(f.isFile()) {
HttpURLConnection c = null;
URL u = null;
try {
u = new URL(url);
c = (HttpURLConnection) u.openConnection();
c.setRequestProperty("Authorization", "Basic " + userpass);
c.setDoOutput(true);
c.setRequestMethod("PUT");
c.setRequestProperty("Content-Type", "multipart/form-data");
FileInputStream fis = new FileInputStream(f);
long fl = f.length();
output(fis, c.getOutputStream(), fl);
} catch (Exception e) {
e.printStackTrace();
}
output(fis, c.getOutputStream(), fl); 将 inputStream 写入 outputStream 然后关闭/刷新两者。
我收到了method not allowed 的回复,有人知道我做错了什么吗?
编辑:(对我来说)他们的响应标头具有:Allow: POST,OPTIONS,GET,HEAD 但使用 curl 的 PUT 有效。
编辑两个:
当使用 curl 我实际上得到:
Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE
但是在使用HttpUrlConnection时,即使我将user-agent设置为curl用户代理,也没有Access-Control-Allow-Methods使用c.getHeaderField()。相反,该字段只是 Allow,并且缺少 PUT。
【问题讨论】:
标签: android curl httpurlconnection put