1

我正在尝试使用以下代码向服务器发出简单的 POST 请求:

NSString *post = [[NSString alloc] initWithFormat:@"email=%@&password=%@", self.email.text, ..]; // .. simplified keychainItem 
NSData *postEncoded = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO]; 
NSString *postLength = [NSString stringWithFormat:@"%d", [postEncoded length]]; 

NSURL *url = [NSURL URLWithString:@"http://eng.studev.groept.be/web2.0/a11_web02/testApp.php"]; 

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
[request setURL:url]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
[request setHTTPBody:postEncoded]; 

self.connection = [NSURLConnection connectionWithRequest:request delegate:self];

在服务器上调试时,这实际上会导致 GET 请求。这就解释了为什么我在尝试读取 $_POST[ ] 变量时遇到 PHP 错误。底线:为什么 setHTTPMethod: 不被接受?

(额外信息:当只是在服务器上用 PHP 编码时,使用 POST 可以正常工作)

4

0 回答 0