首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >android中的HTTP-PUT请求

android中的HTTP-PUT请求
EN

Stack Overflow用户
提问于 2014-12-25 21:45:44
回答 2查看 694关注 0票数 0

我想向亚马逊S3提出一个HTTP请求。它适用于卷曲:

代码语言:javascript
复制
curl -v --upload-file file.jpg 'mybucket.amazonaws.com/avatars/cbe7e51de13c1cf93027ab7e14dbd910.jpg?Expires=1419615226&AWSAccessKeyId={MY_KEY}&Signature={MY_SIGNATURE}'

http头部如下所示:

代码语言:javascript
复制
PUT /avatars/cbe7e51de13c1cf93027ab7e14dbd910.jpg?Expires=1419615226&AWSAccessKeyId={MYKEY}&Signature={MYSIGNATURE} HTTP/1.1\r\n
User-Agent: curl/7.30.0\r\n
Host: mybucket.amazonaws.com\r\n
Accept: */*\r\n
Content-Length: 130485\r\n
    [Content length: 130485]
Expect: 100-continue\r\n
JPEG File Interchange Format

使用环j,我发送以下命令并得到403:

代码语言:javascript
复制
PUT /avatars/cbe7e51de13c1cf93027ab7e14dbd910.jpg?Expires=1419615226&AWSAccessKeyId={MYKEY}&Signature={MYSIGNATURE} HTTP/1.1\r\n
Content-Length: 130745\r\n
    [Content length: 130745]
Content-Type: multipart/form-data; boundary=86VaLUGmO9qAjHzA98n9F2K-5G812B\r\n
Host: mybucket.amazonaws.com\r\n
Connection: Keep-Alive\r\n
Accept-Encoding: gzip\r\n
MIME Multipart Media Encapsulation, Type: multipart/form-data, Boundary: "86VaLUGmO9qAjHzA98n9F2K-5G812B"

通过这样做:

代码语言:javascript
复制
            File myFile = new File("image.jpg");
            RequestParams params = new RequestParams();
            params.put("", myFile,"");
            client.put(MyApplication.getAppContext(),user.avatarUploadUrl, params, responseHandler);

如何像curl一样发送请求(没有内容类型,没有多部分)?因为这似乎对我有用。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-12-26 12:53:51

对我来说很管用:

代码语言:javascript
复制
    File myFile = new File(file);
    RequestParams params = new RequestParams();
    params.put("", myFile);
    StringEntity stringEntity = new StringEntity("Content-Length" + String.valueOf(file.length()));
    FileEntity fileEntity = new FileEntity(myFile,"");
    client.put(MyApplication.getAppContext(), user.avatarUploadUrl, fileEntity, null, responseHandler);
票数 0
EN

Stack Overflow用户

发布于 2014-12-25 22:28:27

我不确定主机应该是什么样子,但我确信,有了这种实现,您可以发送任何您想要的请求。

代码语言:javascript
复制
        Socket s = new Socket();
    String host = "aws.amazon.com";
    PrintWriter s_out = null;
    BufferedReader s_in = null;

        try
        {
        s.connect(new InetSocketAddress(host , 80));
        System.out.println("Connected");

        //writer for socket
            s_out = new PrintWriter( s.getOutputStream(), true);
            //reader for socket
            s_in = new BufferedReader(new InputStreamReader(s.getInputStream()));
        }

        //Host not found
        catch (UnknownHostException e) 
        {
            e.printStackTrace();
        }

        //Send message to server
    String Request = "your request";


    System.out.println("Request sent...");


    String response;
    while ((response = s_in.readLine()) != null) 
    {
        System.out.println( response );
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27650966

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档