1

所以你认为很容易找到 POST 信息到 ASIFormDataRequest 的直接示例,但我还没有找到这些格式的 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]]];`

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

4

1 回答 1

0

与其手动构建可能导致错误的 HTTP 正文,不如使用ASIFormDataRequest.

NSURL *url = [NSURL URLWithString:@"http://IP:PORT/fileXfer"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

[请求 addRequestHeader:@"X-Application-Id" value:@"myID"];
[请求 addRequestHeader:@"X-Transfer-Id" value:@"abcd1234"];

[request setData:[self fileData] withFileName:@"xxx" andContentType:@"application/octet-stream" forKey:@"data"];

[请求 setDelegate:self];
[请求启动异步];
于 2012-06-26T20:43:52.540 回答