4

我在 iPhone 上做一个项目。在这里,我必须向 Web 服务发送 POST 请求。我使用 NSMutableURLRequest 和 NSURLConnection 来发送数据,服务器使用 PHP。我发现检查if ($_SERVER['REQUEST_METHOD'] != "POST")总是失败,因为返回“GET”,即使我在 localhost 上尝试它也是如此。

我的 POST 请求如下所示(通过 Wireshark 嗅探):

POST tester.php HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:10.0.2) Gecko/20100101 Firefox/10.0.2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Referer: http://localhost/tester.php
Content-Type: application/x-www-form-urlencoded
Content-Length: 422

protocol=4&msgtype=authorize&merchant=86189919&language=da&ordernumber=1274168351&amount=100&currency=DKK&continueurl=http%3A%2F%2Fquickpay.net%2Ffeatures%2Fpayment-window%2Fok.php&cancelurl=http%3A%2F%2Fquickpay.net%2Ffeatures%2Fpayment-window%2Ferror.php&callbackurl=http%3A%2F%2Fquickpay.net%2Ffeatures%2Fpayment-window%2Fcallback.php&autocapture=0&cardtypelock=&splitpayment=1&md5check=d2259d9db077e0f5a41b4bf271c3c549

当然,那里有很多虚拟文本,通常不会来自 iPhone,但我尝试通过 html 表单进行相同的调用,并且有效。因此,我复制了所有信息,看看是否会有所作为,但无济于事。

我比较了这两个请求(一个来自 iPhone,另一个来自我的 php 页面),它们看起来完全一样。

我大吃一惊——有人对该怎么做有好的建议吗?

编辑:

您的测试不是针对 post 或 post vs get 进行测试,而是专门针对 POST 进行测试,您可能会遇到案例问题...您是否尝试过在 php 中检查请求本身?– 格雷迪球员

是的,这可能是真的,但该行总是返回 GET,所以如果这就是你的意思,那么案例问题就被涵盖了。

另外,请在 iPhone 发送请求时从服务器发布 print_r($_SERVER) 的结果 – Aleks G

Array( HTTP_HOST => 10.11.105.254 HTTP_USER_AGENT => Mozilla/5.0 (iPhone Simulator; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9B176 HTTP_ACCEPT => text/html,application/xhtml+ xml ,应用程序/xml;q=0.9,/;q=0.8 HTTP_ACCEPT_LANGUAGE => en-us HTTP_ACCEPT_ENCODING => gzip, deflate HTTP_CONNECTION => keep-alive PATH => /usr/bin:/bin:/usr/sbin:/sbin SERVER_SIGNATURE => Apache/2.2.21 (Unix ) DAV/2 PHP/5.3.8 与 Suhosin-Patch 服务器在 10.11.105.254 端口 80 SERVER_SOFTWARE => Apache/2.2.21 (Unix) DAV/2 PHP/5.3.8 与 Suhosin-Patch SERVER_NAME => 10.11.105.254 SERVER_ADDR => 10.11.105.254 SERVER_PORT => 80 REMOTE_ADDR => 10.11.105.254 DOCUMENT_ROOT => /Library/WebServer/Documents SERVER_ADMIN => you@example.com SCRIPT_FILENAME => /Users/tester.php REMOTE_PORT => 51492 GATEWAY_INTERFACE => CGI/ 1.1 SERVER_PROTOCOL => HTTP/1.1 REQUEST_METHOD => GET QUERY_STRING => REQUEST_URI => /tester.php SCRIPT_NAME => /tester.php PHP_SELF => /tester.php REQUEST_TIME => 1334211212 argv =>数组 argc => 0 )

您是否尝试检查是否设置了已知变量,例如 $_POST['known_to_be_set_by_client'] ?确定请求是 POST 还是 GET 的不是 PHP,而是 Web 服务器。——蒂姆·波斯特

当我尝试从 iPhone 请求中回显一个已知变量时,它当然是空的。这是有道理的。当我从表单中回显相同的内容时,它就在那里。

发布您用来发出实际请求的 iOS 代码。– 克里夫·里鲍多

这是创建请求并发送它的代码:

//variables
NSString *protocol = @"4";
NSString *msgtype = @"authorize";
NSString *merchant = @"86189919";
NSString *language = @"da";
NSString *orderNo = @"1274168351";
NSString *amount = @"100";
NSString *currency = @"DKK";
NSString *continueURL = @"http://quickpay.net/features/payment-window/ok.php";
NSString *cancelURL = @"http://quickpay.net/features/payment-window/error.php";
NSString *callbackURL = @"http://quickpay.net/features/payment-window/callback.php";
NSString *autoCapture = @"0";
NSString *cardtypeLock =@"";
NSString *splitText = @"1";
NSString *md5check = @"d2259d9db077e0f5a41b4bf271c3c549";

//setting up the request
NSMutableString *post = [[NSMutableString alloc] initWithString:@"protocol="];
[post appendString:protocol];

[post appendString:@"&msgtype="];
[post appendString:msgtype];

[post appendString:@"&merchant="];
[post appendString:merchant];

[post appendString:@"&language="];
[post appendString:language];

[post appendString:@"&ordernumber="];
[post appendString:orderNo];

[post appendString:@"&amount="];
[post appendString:amount];

[post appendString:@"&currency="];
[post appendString:currency];

[post appendString:@"&continueurl="];
[post appendString:continueURL];

[post appendString:@"&cancelurl="];
[post appendString:cancelURL];

[post appendString:@"&callbackurl="];
[post appendString:callbackURL];

[post appendString:@"&autocapture="];
[post appendString:autoCapture];

[post appendString:@"&cardtypelock="];
[post appendString:cardtypeLock];

[post appendString:@"&splitpayment="];
[post appendString:splitText];

[post appendString:@"&md5check="];
[post appendString:md5check];


//escape characters
NSString *tmp = (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes(
                                        NULL, 
                                        (__bridge CFStringRef)post, 
                                        NULL, 
                                        (CFStringRef)@";/:@+$,~", 
                                        kCFStringEncodingUTF8
                                        );


NSData *body = [tmp dataUsingEncoding:NSUTF8StringEncoding];
//setup url
NSString *urlString =@"http://10.10.225.255/tester.php";
NSURL *quickUrl = [NSURL URLWithString:urlString];

//setup URL request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
//method
[request setHTTPMethod:@"POST"];
//url
[request setURL:url];

//body data to string
NSString *strData = [[NSString alloc]initWithData:body encoding:NSUTF8StringEncoding];

//content length
NSString *postLenght = [NSString stringWithFormat:@"%d", [body length]];

//set body
[request setHTTPBody:body];

//Dummy data to make the request look like the same from the HTML form.
[request setValue:@"localhost" forHTTPHeaderField:@"Host"];
[request setValue:@"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:10.0.2) Gecko/20100101 Firefox/10.0.2" forHTTPHeaderField:@"User-Agent"];
[request setValue:@"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" forHTTPHeaderField:@"Accept"];
[request setValue:@"en-us,en;q=0.5" forHTTPHeaderField:@"Accept-Language"];
[request setValue:@"gzip, deflate" forHTTPHeaderField:@"Accept-Encoding"];
[request setValue:@"http://localhost/tester.php" forHTTPHeaderField:@"Referer"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[request setValue:postLenght forHTTPHeaderField:@"content-length"];

//send the request to server
if([[NSURLConnection alloc] initWithRequest:request delegate:self]){
    NSLog(@"sending data to quickpay"); 
} 
4

2 回答 2

1

(这更像是评论而不是答案)如果

if ($_SERVER['REQUEST_METHOD'] != "POST")

总是失败(正如您在问题中所写),这必须意味着

$_SERVER['REQUEST_METHOD'] == "POST"

所以要么你问错了问题,要么你没有正确验证输入值。请将输出添加var_dump($_SERVER)到您的问题中。

于 2012-04-11T14:01:58.910 回答
0

我想到了。不出所料,这是我自己的疏忽!当我将 url 加载到 UIWebView 中时,在委托中,我使用了 loadRequest 方法,该方法当然使用 GET 请求来返回内容。相反,我应该使用 loadHTMLString:baseURL: 方法来加载返回的数据。这个错误意味着应用程序在我的初始 POST 之后发出了另一个 GET 请求,这导致了错误。很明显,我很久没有注意到这个额外的请求。

于 2012-04-25T07:53:43.443 回答