iOS - 无法使用HTTP Post请求将Multipart表单数据成功发布到服务器

时间:2014-06-30 14:53:17

标签: ios objective-c http-post nsurlconnection nsurlrequest

我正在开发一个iPhone应用程序,我需要将数据从手机传递到服务器。数据采用多部分表格数据的形式。

不幸的是,表单数据没有到达服务器。我尝试了所有方法。但我无法在我的代码中找到问题。我非常确定问题出在客户端,因为我从Android应用程序中点击服务器中的API端点

代码在

之下
NSString *str;
                str=[NSString stringWithFormat:@"https://www.mysite.com/ttk/rest/detail/updat‌​edetail"];

                NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

                [request setURL:[NSURL URLWithString:str]];
                [request setHTTPMethod:@"POST"];

                NSMutableData *body = [NSMutableData data];

                NSString *boundary = @"---------------------------Boundary_1_511262261_1369143433608";

                NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
                [request addValue:contentType forHTTPHeaderField: @"Content-Type"];


                //  parameter imageData
                [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
                [body appendData:[@"Content-Disposition: form-data; name=\"profileimg\"; filename=\"a.png\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
                [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

                NSData *imageData=UIImagePNGRepresentation(pickedImage);

                [body appendData:[NSData dataWithData:imageData]];
                [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

                //  parameter mobilenumber
                [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
                [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"mobileNumber\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

                [body appendData:[mobilenumber dataUsingEncoding:NSUTF8StringEncoding]];
                [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

                //  parameter countryCode
                [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
                [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"countryCode\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

                [body appendData:[countrycode dataUsingEncoding:NSUTF8StringEncoding]];
                [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];


                //  parameter displayName
                [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
                [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"displayName\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

                [body appendData:[strusername dataUsingEncoding:NSUTF8StringEncoding]];
                [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];


                //  parameter mobileID

                [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
                [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"mobileID\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

                NSString *strsa3=[NSString stringWithFormat:@"%@",mobileID];
                [body appendData:[strsa3 dataUsingEncoding:NSUTF8StringEncoding]];
                [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

                //  parameter emailid

                [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
                [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"emailid\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

                [body appendData:[stremail dataUsingEncoding:NSUTF8StringEncoding]];
                [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

                //  parameter Boolean

                [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
                [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"onlycontacts\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
                [body appendData:[searchwithin dataUsingEncoding:NSUTF8StringEncoding]];


                [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];


                //  parameter searchby
                [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
                [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"searchby\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
                if(Radio1Check)
                {//Context
                    [body appendData:[@"Context" dataUsingEncoding:NSUTF8StringEncoding]];
                }
                else
                {
                    [body appendData:[@"Location" dataUsingEncoding:NSUTF8StringEncoding]];
                }

                [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

                //     parameter show location to other

                [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
                [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"showlocation\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

                [body appendData:[location dataUsingEncoding:NSUTF8StringEncoding]];
                [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];


                // close form
                [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];


                [request setHTTPBody:body];

                NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
                NSString *json=[[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
                NSLog(@"Resp string: %@",json);

我设置了结束边界并多次测试此代码。但问题似乎隐藏在我无法找到的代码中。

服务器的返回数据总是为Nil而没有命中服务器..我已经仔细检查了URL并且URL也是正确的。请帮我解决这个问题。谢谢你的时间

1 个答案:

答案 0 :(得分:1)

首先传入一个错误对象并检查它,以找出返回数据为零时出错的地方。这是标准的可可惯例。最后几行应该看起来像

NSError *error; 
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil     error:&error];
if (returnData)
{
        NSString *json=[[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 
        NSLog(@"Resp string: %@",json);
}
else
{
    NSLog(@"Error: %@", error);
}