【问题标题】:How to send a Get request in iOS?如何在 iOS 中发送 Get 请求?
【发布时间】:2011-09-06 22:25:18
【问题描述】:

我正在创建一个库来从具有指定数据和方法类型的特定 URL 获取响应。为此,我正在使用 url 发出请求。但是当我设置它的方法类型时,它显示了[NSURLRequest setHTTPMethod:]中无法识别的选择器发送的异常@ 我将其设置为

[requestObject setHTTPMethod:@"GET"];

告诉我可能是什么问题。如果你有代码,也提供给我。

【问题讨论】:

  • 确保您的 requestObject 是 NSMutableURLRequest 类型。
  • @Imran Raheem:您应该将此作为答案发布,因为这就是解决方案。

标签: iphone objective-c url get nsurl


【解决方案1】:

只需调用和使用:

(void)jsonFetch{

    NSURL *url = [NSURL URLWithString:@"http://itunes.apple.com/us/rss/topaudiobooks/limit=10/json"];

    NSURLSession *session = [NSURLSession sharedSession];

    NSURLSessionDataTask *data = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

        NSError *erro = nil;

        if (data!=nil) {

            NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&erro ];

            if (json.count > 0) {

                for(int i = 0; i<10 ; i++){

                    [arr addObject:[[[json[@"feed"][@"entry"] objectAtIndex:i]valueForKeyPath:@"im:image"] objectAtIndex:0][@"label"]];

                }

            }
        }
        dispatch_sync(dispatch_get_main_queue(),^{

            [table reloadData];
        });
    }];

    [data resume];
}

【讨论】:

    【解决方案2】:
    NSMutableURLRequest *request = 
    [NSMutableURLRequest requestWithURL:[NSURL 
                URLWithString:serverAddress] 
                cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
                                                       timeoutInterval:10
     ];
    
    [request setHTTPMethod: @"GET"];
    
    NSError *requestError = nil;
    NSURLResponse *urlResponse = nil;
    
    
    NSData *response1 =
            [NSURLConnection sendSynchronousRequest:request
                             returningResponse:&urlResponse error:&requestError];
    

    【讨论】:

    • 如何处理 NSData 响应?
    【解决方案3】:

    NSString *getString = [NSString stringWithFormat:@"parameter=%@",yourvalue];
    NSData *getData = [getString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *getLength = [NSString stringWithFormat:@"%d", [getData length]];
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:@"https:yoururl"]];
    [request setHTTPMethod:@"GET"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:getData];
    self.urlConnection = [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
    NSAssert(self.urlConnection != nil, @"Failure to create URL connection.");
    // show in the status bar that network activity is starting [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    【讨论】:

    • @simpleBob 也不适用于我......我让它在 POST 上工作得很好,但不是 GET。然后我检查了 setHTTPBody: 的文档,其中声明了数据参数“接收者的新请求正文。这是作为请求的消息正文发送的,就像在 HTTP POST 请求中一样。”
    • @StephSharp 我用这样的东西,也许它对你有帮助:NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:WS_URL]]; [request setHTTPMethod:@"GET"]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    • 网址应该是带参数的网址。类似http://my.URL.com/test?param1=1&amp;param2=2
    • @simpleBob 感谢您的代码,这几乎就是我最终所做的!
    • 错字:[request setValue:postLength... 应该是[request setValue:getLength...
    【解决方案4】:

    确保您的 requestObject 是 NSMutableURLRequest 类型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-30
      • 1970-01-01
      • 2015-02-11
      • 1970-01-01
      • 2012-10-24
      • 2010-10-31
      • 1970-01-01
      相关资源
      最近更新 更多