gpt4 book ai didi

java - Android 中的 HTTP-PUT 请求

转载 作者:行者123 更新时间:2023-12-01 12:05:35 25 4
gpt4 key购买 nike

我想向 Amazon S3 发出 HTTP-PUT 请求。它与curl一起使用:

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

http header 如下所示:

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

loopj我发送以下内容并收到 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"

通过这样做:

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

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

最佳答案

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

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

关于java - Android 中的 HTTP-PUT 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27650966/

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