3

我创建了一个 Web 服务来在 iOS 应用程序和 Joomla 网站之间进行通信,并且我使用 GET 方法在移动应用程序和 Web 服务之间以及 Web 服务和控制器之间进行通信(PHP 文件确实工作并返回数据),但我没有找到如何将实现转换为 POST 方法这里是实际系统:

ws.php:这是网络服务(简单示例)

<?php 
      $id = $_GET['id'] ; // get the data from the URL

// here i make testes

// then I redirect to the controller of the Joomla component that receive 
// the call of the request the URL is actually the attribute "action" of 
// an existing HTML Form that implement the login system, I added a 
// parameter called web service to help me to modify the controller 
// to make the difference between a normal call and a web service call

header("Location: index.php?option=com_comprofiler&task=login&ws=1&id=1");
?> 

Controller.php:网络服务调用和网络调用的接收者(来自浏览器)

<?php 
// code added by me, in the existent controller of the component
// it was waiting for a form submitting, so I got to convert my data to POST here

if (isset($_GET['ws'])) // it's a web service call 
{
   $_POST['id'] = $_GET['id'] ; 

   // do the task ... 

   if ($correctLogin) // just an example 
     echo "1"
   else 
     echo '0';
}
?>

我没有放真正的实现,只是系统的一个简单的例子,但都是一样的

从手机拨打

 NSURL *url = [[NSURL alloc]initWithString:@"http://localhost/ws.php?id=1"];

 NSData *dataUrl= [NSData dataWithContentsOfURL:url];

 NSString *str = [[NSString alloc]initWithData:dataUrl 
                                      encoding:NSUTF8StringEncoding];

if(![str isEqualToString:@"0"])
    NSLog(@"connected");
else
    NSLog(@"not connected");

所以我不想使用 GET 方法,我只想使用 POST 从手机接收我的数据,并使用 POST 将数据发送到控制器,最好的解决方案是什么?

4

1 回答 1

1

如果您希望您的应用程序使用 POST 方法发送数据,那么我就是这段代码。我希望它会有所帮助。

它需要在字典对象中发送数据。将要发送的数据编码为 POST,然后返回响应(如果您想要字符串格式的结果,您可以使用 [[NSString alloc] initWithData:dresponse encoding: NSASCIIStringEncoding]; 返回数据时)

-(NSData*) getData:(NSDictionary *) postDataDic{

    NSData *dresponse = [[NSData alloc] init];

    NSURL *nurl = [NSURL URLWithString:url];
    NSDictionary *postDict = [[NSDictionary alloc] initWithDictionary:postDataDic];
    NSData *postData = [self encodeDictionary:postDict];

    // Create the request
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:nurl];
    [request setHTTPMethod:@"POST"]; // define the method type
    [request setValue:[NSString stringWithFormat:@"%d", postData.length] forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];

        // Peform the request
        NSURLResponse *response;
        NSError *error = nil;

        dresponse = [NSURLConnection sendSynchronousRequest:request  
                                                     returningResponse:&response
                                                                 error:&error];

    return dresponse;
}

此方法为 POST 准备 Dictionary 数据

- (NSData*)encodeDictionary:(NSDictionary*)dictionary {
    NSMutableArray *parts = [[NSMutableArray alloc] init];
    for (NSString *key in dictionary) {
        NSString *encodedValue = [[dictionary objectForKey:key] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSString *encodedKey = [key stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
        NSString *part = [NSString stringWithFormat: @"%@=%@", encodedKey, encodedValue];
        [parts addObject:part];
    }
    NSString *encodedDictionary = [parts componentsJoinedByString:@"&"];
    return [encodedDictionary dataUsingEncoding:NSUTF8StringEncoding];
}
于 2013-02-09T21:06:16.047 回答