【问题标题】:Convert a Web service from GET to POST将 Web 服务从 GET 转换为 POST
【发布时间】:2013-02-09 20:21:17
【问题描述】:

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

【问题讨论】:

  • 你不能只使用$_REQUEST,因为它同时处理$_POST$_GET
  • 我不想从一开始就使用$_GET,我只想使用$_POST从手机发送数据,也从网络服务发送数据到控制器。在实际系统中,我使用重定向从两个 PHP 文件发送数据,但我想更改它并使用 $_POST 在两个 PHP 文件之间以及移动应用程序和 Web 服务之间发送数据

标签: ios web-services post joomla get


【解决方案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 准备字典数据

- (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];
}

【讨论】:

  • 谢谢,这很有用,但只是在iOS实现端,我还是需要把web服务从使用GET改为POST。
  • 您正在重定向到其他 url,因此您无法通过 url 发送帖子数据,您可以使用 curl 获取结果,但另一种解决方案是将您的 POST 数据转储到会话变量中,例如 ( $_SESSION['post'] = $_POST;) 然后在你的控制器中检查 $_SESSION['post'] 是否有数据,如果没有数据正常,否则 $_POST = $_SESSION['post'];并取消设置($_SESSION['post']);
  • 再次感谢您的评论,因为我知道会话变量是客户端,因为如果我没有错,它将数据存储在 cookie 中,并且我的请求将来自移动设备而不是浏览器!如果我错了,请纠正我?但是cURL的东西我认为它可以工作,基本上我需要提交/发送现有html表单的action属性的数据
  • 会话存储在服务器端,而不是客户端,但是如果您想为用户维护相同的会话,请存储用户会话 ID(例如在 db 表中)并使用它来加载特定的每个用户的会话。但由于您关心 POST,您可以使用 session 传递 POST 数据,然后丢弃 session 中的数据。您的组件如何返回数据,它是否与 GET + 重定向一起使用?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-02
  • 2020-06-21
  • 1970-01-01
相关资源
最近更新 更多