如何在 Objective c 中使用 NSURLSESSION 将多个值传递给服务器
How to pass the multiple values to the server using NSURLSESSION in Objective c
我想将多个值传递到我的服务器,例如 1.phonenum、2.name 3.details 4.email.etc...
我的服务器正在 PHP 上工作。我想根据字段的键值将数据传递给。
phone 号码 U_pnum:123456789 姓名 U_name:naveen 详细信息 U_details:developer 电子邮件 U_email:xyz@gmail.com .
我想使用 NSUrlSession 发送数据。
我写了一些代码......但我没有从我的服务器得到任何响应。
请帮帮我。
谢谢。
试试这个
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];
NSURL * url = [NSURL URLWithString:@"http://yourServerUrl/postFile.php"];
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
NSString * params =@"U_pnum=valueofU_pnum&U_name=valueofU_name&U_details=valueofU_details&U_email=valueofU_email";
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"Response:%@ %@\n", response, error);
if(error == nil)
{
NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(@"Data = %@",text);
}
}];
[dataTask resume];
我想将多个值传递到我的服务器,例如 1.phonenum、2.name 3.details 4.email.etc...
我的服务器正在 PHP 上工作。我想根据字段的键值将数据传递给。
phone 号码 U_pnum:123456789 姓名 U_name:naveen 详细信息 U_details:developer 电子邮件 U_email:xyz@gmail.com .
我想使用 NSUrlSession 发送数据。 我写了一些代码......但我没有从我的服务器得到任何响应。
请帮帮我。
谢谢。
试试这个
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];
NSURL * url = [NSURL URLWithString:@"http://yourServerUrl/postFile.php"];
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
NSString * params =@"U_pnum=valueofU_pnum&U_name=valueofU_name&U_details=valueofU_details&U_email=valueofU_email";
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"Response:%@ %@\n", response, error);
if(error == nil)
{
NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(@"Data = %@",text);
}
}];
[dataTask resume];