objective-c URL GET/POST 请求objective-c
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2019128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
URL GET/POST Request objective-c
提问by kosmaks
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:
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
sry 我的英语
回答by Nikolai Ruhe
You are using a plain NSStringliteral where an NSURLobject is expected: [...] initWithURL:@"http://localhost/wsh/index.php"[...]
您NSString在需要NSURL对象的地方使用普通文字:[...] initWithURL:@"http://localhost/wsh/index.php"[...]
Change this to initWithURL:[NSURL URLWithString:@"http://localhost/wsh/index.php"].
将此更改为initWithURL:[NSURL URLWithString:@"http://localhost/wsh/index.php"].
回答by epatel
I wonder if also you should switch the value and key for the post values, ie change the line
我想知道您是否也应该切换帖子值的值和键,即更改行
[request setPostValue:@"option" forKey:@"myFormField1"];
to
到
[request setPostValue:@"myFormField1" forKey:@"option"];

