目前我正在向服务器发出请求(出于隐私原因,该服务器将保持未命名状态)该 url 接收一封电子邮件,用于回显您传递给它的变量参数。我的问题是我不确定最有效的方法。因此,我这样做的一种方法是仅传递一个表单变量,但我不确定如何发送多个表单变量的方法:
NSData * postData;
NSString * postLength;
NSString * sample= @"helloWorld";
//Need help here. How would I go about dynamiaclly adding multiple form vars?
NSString * token = [NSString stringWithFormat:@"username=%@,",sample];
[[NSURLCache sharedURLCache]removeAllCachedResponses];
postData = [token dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
postLength = [NSString stringWithFormat:@"%d",[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:dbUrl]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSLog(@"Server Request : %@ ",request);
NSError *error1;
NSURLResponse *response;
returning the data in a urlData object
NSData *urlData =[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error1];
NSString *responseString = [[NSString alloc] initWithData:urlData encoding:NSASCIIStringEncoding];
NSLog(@"Token Response : %@ ",responseString);
然后我意识到另一种方法是使用 coco pod AFNetworking。 AFNetworking 有一个选项,它按照我想要的方式工作!问题是它使用的方法已经贬值了。我必须破解它才能按照我想要的方式工作,但这是我调用的方法来实现它。
UITextField * username = [alertView textFieldAtIndex:0];
UITextField *password = [alertView textFieldAtIndex:1];
NSDictionary *parameters = @{@"ME:": username.text, @"MY Message": password.text};
NSString * txt = username.text;
[[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:dbUrl parameters:parameters];
这是 AFSerialization.h 中已弃用的方法
- (NSMutableURLRequest *)requestWithMethod:(NSString *)method
URLString:(NSString *)URLString
parameters:(id)parameters ;
希望我能清楚地让大家理解我的问题。谢谢!
最佳答案
您可以轻松地附加更多帖子变量:
NSString * token =
[NSString stringWithFormat:@"username=%@&password=%@",sample, pass];
关于ios - 如何在 iOS 中适本地发布多个表单变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24996918/