gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:51:16 24 4
gpt4 key购买 nike

我正在开发一个 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 也是正确的。请帮我解决这个问题。感谢您的宝贵时间

最佳答案

首先传入一个错误对象并检查它以找出当返回的数据为 nil 时出了什么问题。这是标准的 cocoa 约定。最后几行应该类似于

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);
}

关于iOS - 无法使用 HTTP 发布请求将多部分表单数据成功发布到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24492971/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com