通过xcode的多部分HTTP请求 [英] Multi-Part HTTP Request through xcode

查看:89
本文介绍了通过xcode的多部分HTTP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将图像,视频和音频文件上传到服务器.我已阅读此线程关于类似的主题,但无法完全理解代码流.如果您可以向我建议一些示例代码或教程,那就太好了.我正在使用以下代码在没有任何媒体的情况下连接到服务器

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.

提前谢谢!

推荐答案

虽然回答我自己的问题还为时过早,但是我得到了解决方案,因此想到了将其添加到这里.

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.

:)

这篇关于通过xcode的多部分HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆