我创建了一个 Web 服务来在 iOS 应用程序和 Joomla 网站之间进行通信,我使用 GET 方法在移动应用程序和 Web 服务之间以及 Web 服务和 Controller (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 将数据发送到 Controller ,什么是最好的解决方案?
最佳答案
如果您希望您的应用使用 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,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14791169/