【问题标题】:URL GET/POST Request objective-cURL GET/POST 请求objective-c
【发布时间】:2023-04-02 17:32:01
【问题描述】:

我必须向 localhost 发送 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

对不起我的英语

【问题讨论】:

    标签: objective-c url post get request


    【解决方案1】:

    您正在使用一个普通的 NSString 文字,其中需要一个 NSURL 对象:[...] initWithURL:@"http://localhost/wsh/index.php" [...]

    将其更改为initWithURL:[NSURL URLWithString:@"http://localhost/wsh/index.php"]

    【讨论】:

      【解决方案2】:

      我想知道您是否也应该切换帖子值的值和键,即更改行

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

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

      【讨论】:

        猜你喜欢
        • 2017-02-11
        • 2016-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-25
        • 2015-03-15
        • 1970-01-01
        • 2014-07-24
        相关资源
        最近更新 更多