Multi-Part HTTP Request through xcode

怎甘沉沦 提交于 2019-12-08 07:20:49

问题


i want to upload image,video and audio files to a server. I have read this thread on the similar topic but wasn't able to understand completely the flow of the code. It would be great if you can suggest me some sample code or tutorial to start with. I am using the following code to connect without any media to the server

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSString *url =[[NSString alloc]initWithFormat:@"%@",[NetworkConstants getURL]];
NSURL *theURL =[NSURL URLWithString:url];
[url release];
NSMutableURLRequest *theRequest =[NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:0.0f];
[theRequest setHTTPMethod:@"POST"];

NSString *theBodyString = [NSString stringWithFormat:@"json1=%@&userID=%@",jsonObject,[GlobalConstants getUID]];

NSData *theBodyData = [theBodyString dataUsingEncoding:NSUTF8StringEncoding];
[theRequest setHTTPBody:theBodyData];

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

if (conn) {
    NSLog(@"Successful in sending sync");
}
else {
    NSLog(@"Failed Connection in sending sync");
}
[conn release];

It would be really convenient for me if anything could be done editing this part of code.

Any form of help would be highly appreciated.

Thanks in advance!!


回答1:


Although it is very early to answer my own question but I got the solution so thought of adding it up here.

To the above code we just need the following modification

        NSData *imageData = UIImageJPEGRepresentation(attachedImage.image, 90);
        NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
        NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
        [theRequest addValue:contentType forHTTPHeaderField:@"Content-Type"];


        NSMutableData *theBodyData = [NSMutableData data];
        [theBodyData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [theBodyData appendData:[@"Content-Disposition: form-data; name= \"server_value_name\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        [theBodyData appendData:[yourString dataUsingEncoding:NSUTF8StringEncoding]];
        //this appends the image data
        [theBodyData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [theBodyData appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"image\"; filename=\"1.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [theBodyData appendData:[[NSString stringWithString:@"Content-Type: image/jpg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [theBodyData appendData:[NSData dataWithData:imageData]];
        [theBodyData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [theRequest setHTTPBody:theBodyData];

And the rest remains the same as in Question.

One just need to remember while sending a Multi-Part Request that all the parameters required by the server need to go with in the boundary and every parameter should be send in individual boundaries.

Hope this helps the others as well.

:)



来源:https://stackoverflow.com/questions/5229982/multi-part-http-request-through-xcode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!