我编写了一个 iOS 应用程序,它通过 POST 成功地将字符串上传到服务器。
bodyString = [NSString stringWithFormat:@"text1=%@&submit1=+Absenden+", contentString];
request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.xxx /xxx/get.jsp"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[bodyString dataUsingEncoding:NSUTF8StringEncoding]];
self.connection = [NSURLConnection connectionWithRequest:request delegate:self];
我需要更改服务器,所以我使用了相同的代码,但它不起作用,虽然我得到了 200 状态代码。负责新服务器的人说它不起作用,因为我正在发送 POST 但我没有将字符串作为 POST 参数发送。
不幸的是,她不了解 iOS,因此无法向我解释如何操作。我四处搜索,但我什么也没找到,仍然不明白:发送 POST 和同时发送一个字符串作为 POST 参数是什么意思?
任何帮助都会非常友好。
最佳答案
借助 NSURLConnection 委托(delegate)方法处理您的结果数据
NSString *post = [NSString stringWithFormat:@"postBody=%@",@"Raja"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[post length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://localhost/promos/index.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
NSURLConnection *theConnection = [NSURLConnection connectionWithRequest:request delegate:self];
if( theConnection ){
// indicator.hidden = NO;
mutableData = [[NSMutableData alloc]init];
}
你的 PHP 代码
<?php
$result = $_POST['postBody'];
echo $result;
?>
关于ios发送带有POST参数的POST请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21305931/