URL GET/POST 请求 Objective-c
我必须
<?php
if(@$_GET['option']) {
echo "You said \"{$_GET['option']}\"";
}else if(@$_POST['option']) {
echo "You said \"{$_POST['option']}\"";
}
?>
使用此代码向 localhost: ive 发送 get 或 post 请求:
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
对不起我的英语
I have to send get or post request to localhost:
<?php
if(@$_GET['option']) {
echo "You said \"{$_GET['option']}\"";
}else if(@$_POST['option']) {
echo "You said \"{$_POST['option']}\"";
}
?>
ive using this code:
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];
it works, but one time in code. if ill do it another one time, application has terminate.
Im try to use 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");
}
it says:
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
sry for my english
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在使用纯
NSString
文字,其中需要NSURL
对象: [...]initWithURL:@"http://localhost/wsh/index.html php"
[...]将其更改为
initWithURL:[NSURL URLWithString:@"http://localhost/wsh/index.php"]
。You are using a plain
NSString
literal where anNSURL
object is expected: [...]initWithURL:@"http://localhost/wsh/index.php"
[...]Change this to
initWithURL:[NSURL URLWithString:@"http://localhost/wsh/index.php"]
.我想知道您是否还应该切换后值的值和键,即将行更改
为
I wonder if also you should switch the value and key for the post values, ie change the line
to