简体   繁体   中英

HTTP-PUT Request in android

I want to make an HTTP-PUT request to Amazon S3. It works with curl:

curl -v --upload-file file.jpg 'mybucket.amazonaws.com/avatars/cbe7e51de13c1cf93027ab7e14dbd910.jpg?Expires=1419615226&AWSAccessKeyId={MY_KEY}&Signature={MY_SIGNATURE}'

the http header looks like this:

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

With loopj I send the following and get 403:

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"

by doing this:

            File myFile = new File("image.jpg");
            RequestParams params = new RequestParams();
            params.put("", myFile,"");
            client.put(MyApplication.getAppContext(),user.avatarUploadUrl, params, responseHandler);

How can I send a request like curl does with java (no contenttype, no multipart) ? Because this seems to work for me.

i am not sure exactly howthe host should look like, but i am sure that with this kind of implementation you can send any request you want.

        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 );
    }

That worked for me:

    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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM