objective-c - URL GET/POST 请求 objective-c

标签 objective-c url post get request

我必须向本地主机发送 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/

相关文章:

php - 在 PHP 中将字符串转换为 URL

url - 具有外国编码字符的 SEO 友好 URL 参数?

php - mvc 中的 url 重定向。我想将 localhost/windshield-replacement.html 重定向到 localhost/greenvalleyaz

ios - 使用 sizeWithAttribute 获取 UITextView 的确切大小

iphone - 有什么方法可以在不捕获整个屏幕的情况下将 UIView 保存为 png

objective-c - MVC 面向对象

asp.net-mvc - 使用相同的方法签名发布和获取

Objective-C 多重初始化器

php - 不使用表单设置 POST 变量

javascript - 使用 fetch 将跨域 JSON 数据发布到 Express 后端