多部分POST请求直接在ASIFormDataRequest中(multipart POST request directly in ASIFormDataRequest)

系统教程 行业动态 更新时间:2024-06-14 17:04:02
多部分POST请求直接在ASIFormDataRequest中(multipart POST request directly in ASIFormDataRequest)

所以你认为找到ASIFormDataRequest的POST信息的直接例子很容易,但我没有找到这些格式的1:1。 这是我要发送的POST请求:

POST /fileXfer HTTP/1.1 Content-Type: multipart/form-data; boundary=AaB03x Content-Length: 200 AppID:myID TransferID:abcd1234 -- AaB03x Content-Disposition: form-data; name="data"; filename="xxx" Content-Type: application/octet-stream Content-Length: 100 ... contents of file ... --AaB03x--

编辑:

事实证明,我的问题更多的是服务器接收数据。 但这就是我最终的结果!

NSURL *url = [NSURL URLWithString:@"http://IP:PORT/fileXfer"]; ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; [request setPostFormat:ASIMultipartFormDataPostFormat]; //Sets headers of POST [request addRequestHeader:@"X-Application-Id" value:@"myID"]; [request addRequestHeader:@"X-Transfer-Id" value:@"abcd1234"]; //Sets data NSData *fileData = [NSData dataWithContentsOfFile:kEncryptedFilePath]; [request setData:fileData withFileName:@"xxx" andContentType:@"application/octet-stream" forKey:@"data"]; [request setDelegate:self]; [request startAsynchronous];

我还在ASIFormDataRequest的buildMultipartFormDataPostBody中添加了以下行,其中设置了Content-Disposition和Content-Type:

[self appendPostString:[NSString stringWithFormat:@"Content-Length: %lu\r\n\r\n", [data length]]];`

我不确定是否有更简单的方法直接从请求中添加该行,但是在我尝试的事情中,这似乎是有效的。

So you think it would be easy to find a direct example of POST information to ASIFormDataRequest, but I havent found a 1:1 in these formats. Here's the POST request I want to send:

POST /fileXfer HTTP/1.1 Content-Type: multipart/form-data; boundary=AaB03x Content-Length: 200 AppID:myID TransferID:abcd1234 -- AaB03x Content-Disposition: form-data; name="data"; filename="xxx" Content-Type: application/octet-stream Content-Length: 100 ... contents of file ... --AaB03x--

EDIT:

Turns out my problems were more with the server receiving the data. But this is what I ended up with!

NSURL *url = [NSURL URLWithString:@"http://IP:PORT/fileXfer"]; ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; [request setPostFormat:ASIMultipartFormDataPostFormat]; //Sets headers of POST [request addRequestHeader:@"X-Application-Id" value:@"myID"]; [request addRequestHeader:@"X-Transfer-Id" value:@"abcd1234"]; //Sets data NSData *fileData = [NSData dataWithContentsOfFile:kEncryptedFilePath]; [request setData:fileData withFileName:@"xxx" andContentType:@"application/octet-stream" forKey:@"data"]; [request setDelegate:self]; [request startAsynchronous];

I also added the following line to ASIFormDataRequest's buildMultipartFormDataPostBody where Content-Disposition and Content-Type are set:

[self appendPostString:[NSString stringWithFormat:@"Content-Length: %lu\r\n\r\n", [data length]]];`

I'm not sure if there's an easier way to add that line directly from the request, but out of the things I tried, this seemed to be the one that worked.

最满意答案

您可以更好地使用ASIFormDataRequest提供的方法,而不是手动构建HTTP主体,这可能会导致错误。

NSURL *url = [NSURL URLWithString:@"http://IP:PORT/fileXfer"]; ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; [request addRequestHeader:@"X-Application-Id" value:@"myID"]; [request addRequestHeader:@"X-Transfer-Id" value:@"abcd1234"]; [request setData:[self fileData] withFileName:@"xxx" andContentType:@"application/octet-stream" forKey:@"data"]; [request setDelegate:self]; [request startAsynchronous];

Rather than building the HTTP body manually, which may lead to errors, you should better use the methods provided by ASIFormDataRequest.

NSURL *url = [NSURL URLWithString:@"http://IP:PORT/fileXfer"]; ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; [request addRequestHeader:@"X-Application-Id" value:@"myID"]; [request addRequestHeader:@"X-Transfer-Id" value:@"abcd1234"]; [request setData:[self fileData] withFileName:@"xxx" andContentType:@"application/octet-stream" forKey:@"data"]; [request setDelegate:self]; [request startAsynchronous];

更多推荐

本文发布于:2023-04-24 20:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/e813d942cc93d86d53ed6496fa94cf13.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ASIFormDataRequest   POST   request   multipart

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!