将Web服务从GET转换为POST

时间:2013-02-09 20:21:18

标签: ios web-services post joomla get

我创建了一个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将数据发送到控制器,最佳解决方案是什么?

1 个答案:

答案 0 :(得分:1)

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

它将数据发送到字典对象中。 对要作为POST发送的数据进行Ecode 然后返回响应(如果你想要字符串格式的结果你可以使用[[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

准备字典数据
- (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];
}