我正在尝试学习如何在 PHP 和 Objective-c 中使用 POST 和 GET 的东西。
我遇到了一个问题。在objective-c中进行POST后,如何获得返回结果?
我的 PHP 文件是:
<html>
<head><title>Bug Reporter</title></head>
<body>
<h2>Fancy Bug Reporter</h2>
<hr />
<?php
if (isset($_POST['go']))
{
// Form was posted
print "User submitted bug: ". $_POST['name'] . ": " . $_POST['description'];
}
else
{
// Form was not posted, display form
?>
<form method="POST" action="<?php echo $PHP_SELF; ?>">
Bug Name:<br /><input type="text" name="name" maxlength="100" /><br />
Bug Description:<br /><input type="text" name="description" maxlength="1000" size="80" /><br />
<input type="submit" name="go" value="Add Bug">
</form>
<?php
}
?>
</body>
</html>
在目标 c 中:
NSMutableURLRequest *request =
[[NSMutableURLRequest alloc] initWithURL:
[NSURL URLWithString:@"http://xxxxx.com/xxx.php"]];
[request setHTTPMethod:@"POST"];
NSString *postString = @"go=1&name=Bad%20Bad%20Bug&description=This%20bug%20is%20really%20really%20super%20bad.";
[request setValue:[NSString
stringWithFormat:@"%d", [postString length]]
forHTTPHeaderField:@"Content-length"];
[request setHTTPBody:[postString
dataUsingEncoding:NSUTF8StringEncoding]];
[[NSURLConnection alloc]
initWithRequest:request delegate:self];