URL GET/POST 请求 Objective-c

发布于 2024-08-16 17:50:11 字数 1229 浏览 5 评论 0原文

我必须

<?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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白鸥掠海 2024-08-23 17:50:11

您正在使用纯 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 an NSURL object is expected: [...] initWithURL:@"http://localhost/wsh/index.php" [...]

Change this to initWithURL:[NSURL URLWithString:@"http://localhost/wsh/index.php"].

知足的幸福 2024-08-23 17:50:11

我想知道您是否还应该切换后值的值和键,即将行更改

[request setPostValue:@"option" forKey:@"myFormField1"];

[request setPostValue:@"myFormField1" forKey:@"option"];

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"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文