嘻嘻,
我的 NSURLRequest HTTP POST 出现问题,我无法找出问题所在。
这是我的服务器端 php:
<html>
<head>
</head>
<body>
<?php
if (! isset($_POST['sometext'])) {
echo "NOT SET";
} else {
echo $_POST['sometext'];
}
?>
</body></html>
这是我发布数据的目标 c 代码:
NSString *urlString = @"http://localhost:8888/postpage";
NSURL *url = [[NSURL alloc] initWithString:urlString];
NSMutableURLRequest *urlRequest=[NSMutableURLRequest requestWithURL:url];
[url release];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[urlRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
NSString *postString = @"sometext=Hello&language=en";
NSString *msgLength = [NSString stringWithFormat:@"%d", [postString length]];
[urlRequest setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
NSString *returnString = [[NSString alloc] initWithData:[NSURLConnection sendSynchronousRequest:urlRequest returningResponse:nil error:nil] encoding:NSUTF8StringEncoding];
NSLog(@"%@",returnString);
NSLog 的结果是:
<html>
<head>
</head>
<body>
NOT SET
</body>
</html>
已经谢谢了