我必须向本地主机发送 get 或 post 请求:
<?php
if(@$_GET['option']) {
echo "You said \"{$_GET['option']}\"";
}else if(@$_POST['option']) {
echo "You said \"{$_POST['option']}\"";
}
?>
我使用这段代码:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost/wsh/index.php?option=Hello"]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *get = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
它有效,但一次是在代码中。如果我再做一次,应用程序就终止了。
我尝试使用 ASIFormDataRequest:
ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:@"http://localhost/wsh/index.php"] autorelease];
[request setPostValue:@"option" forKey:@"myFormField1"];
[request start];
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
NSLog(response);
}else{
NSLog(@"error");
}
它说:
2010-01-07 13:20:34.964 WSH[3351:903] -[NSCFString absoluteURL]: unrecognized selector sent to instance 0x160f8
2010-01-07 13:20:34.966 WSH[3351:903] error
对不起我的英语
最佳答案
您正在使用一个普通的 NSString
文字,其中需要一个 NSURL
对象:[...] initWithURL:@"http://localhost/wsh/index.php"
[...]
将其更改为 initWithURL:[NSURL URLWithString:@"http://localhost/wsh/index.php"]
。
关于objective-c - URL GET/POST 请求 objective-c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2019128/