所以你认为找到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];更多推荐
发布评论