Ios 将Web服务从GET转换为POST

Ios 将Web服务从GET转换为POST,ios,web-services,post,joomla,get,Ios,Web Services,Post,Joomla,Get,我创建了一个web服务,用于在iOS应用程序和Joomla网站之间进行通信,我使用GET方法在移动应用程序和web服务之间以及web服务和控制器之间进行通信(执行工作并返回数据的PHP文件),但我没有找到如何将实现转换为POST方法这是实际的系统: ws.php:它是web服务(简单示例) 因此,我不想使用GET方法,我只想使用POST从手机接收数据,并使用POST将数据发送给控制器,最好的解决方案是什么?如果您希望您的应用程序使用POST方法发送数据,那么我就是这个代码。我希望这会有帮助 它接

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

ws.php:它是web服务(简单示例)


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

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

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

此方法为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];
}

你不能只使用
$\u REQUEST
来处理
$\u POST
$\u GET
吗?我不想从一开始就使用
$\u GET
,我只想使用
$\u POST
从手机发送数据,还想从web服务发送到控制器。在实际系统中,我使用重定向从两个PHP文件发送数据,但我想改变这一点,并使用
$\u POST
在两个PHP文件之间以及移动应用程序和web服务之间发送数据谢谢,这很有用,但仅在iOS实现端,我仍然需要更改使用GET to POST的web服务。您正在重定向到其他url,因此无法通过url发送帖子数据,您可以使用curl获取结果,但另一种解决方案是将帖子数据转储到会话变量中,如($\u session['POST']=$\u POST;),然后在控制器中检查$\u session['POST']是否有数据,如果没有数据表现正常,则$\u POST=$\u会话['POST'];和取消设置($_会话['post']);再次感谢您的评论,因为我知道会话变量是客户端变量,因为如果我没有错的话,它将数据存储在cookie中,我的请求将来自手机而不是浏览器!如果我错了,请纠正我?但是cURL的东西我认为它可以工作,基本上我需要提交/发送现有html formSession的action属性的数据存储在服务器端,而不是客户端,但是如果您想为用户维护相同的会话,请存储用户会话id(例如,在db表中)并使用它为每个用户加载特定会话。但由于您关心POST,所以可以使用会话传递POST数据,然后从会话中丢弃数据。组件是如何返回数据的,它是否与GET+重定向一起工作?
<?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");
-(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;
}
- (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];
}