如何在 HTTP post 中将多个参数发送到 PHP 服务器 [英] How to send multiple parameterts to PHP server in HTTP post

查看:20
本文介绍了如何在 HTTP post 中将多个参数发送到 PHP 服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在向 php 服务器发送 base64 字符串并且它运行良好.现在我想将另一个参数作为字符串发送.谁能告诉我需要在下面的代码中添加什么代码.

I'm sending base64 string to php server and its working well. Now I want to send another parameter as a string. Can anyone tell me what code need to add in below code.

以下代码适用于单个参数.我们如何为多个参数修改它?

Below code is working good for single parameter. How can we modify it for multiple parameters?

 NSData *data = [UIImageJPEGRepresentation(imgeview.image,90) base64Encoding];

// Create your request string with parameter name as defined in PHP file
NSString *myRequestString = [NSString stringWithFormat:@"question_image=%@",data];
myRequestString = [myRequestString stringByReplacingOccurrencesOfString:
                                             @"+" withString:@"%2B"];

// Create Data from request
NSData *myRequestData = [NSData dataWithBytes:[myRequestString UTF8String] 
                                       length:[myRequestString length]];
request = [[NSMutableURLRequest alloc] initWithURL:
    [NSURL URLWithString:@"http://192.168.0.101/Mobile_tutor/webservice/question_details.php"]];
// set Request Type
[request setHTTPMethod:@"POST"];
// Set content-type
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
// Set Request Body
[request setHTTPBody:myRequestData];
// Now send a request and get Response
NSData *returnData = [NSURLConnection sendSynchronousRequest:request 
                                           returningResponse:nil 
                                                       error: nil];
// Log Response
NSString *response = [[NSString alloc] initWithBytes:[returnData bytes] 
                                              length:[returnData length]
                                            encoding:NSUTF8StringEncoding];
NSLog(@"-------------%@",response); // here you get reasponse string

推荐答案

对于网络操作,这些更好地支持 API,如 AFNetworking 可用的女巫异步工作和更好的处理方式

For the network operation these is better supporting API like AFNetworking available witch work async and way better to handle

AFNetworking 教程

从这里获取

NSArray *keys = @[@"UserID", ];
NSArray *objects = @[@(userId)];

NSDictionary *parameter = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:
                            [NSURL URLWithString:BaseURLString]];
[httpClient setParameterEncoding:AFJSONParameterEncoding];
[httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];

NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST"
                                                        path:@"services/UserService.svc/GetUserInfo"
                                                  parameters:parameter];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    NSError* error = nil;
    id jsonObject = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error];
    if ([jsonObject isKindOfClass:[NSDictionary class]]) {
        // do what ever
    }
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

}];

这篇关于如何在 HTTP post 中将多个参数发送到 PHP 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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