如何在iOS中发送Get请求?

San*_*ush 26 iphone url get objective-c nsurl

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

[requestObject setHTTPMethod:@"GET"];
Run Code Online (Sandbox Code Playgroud)

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

Mob*_*oid 82

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];
Run Code Online (Sandbox Code Playgroud)


Ada*_*V C 12

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;