How to send a Get request in iOS?

后端 未结 4 725
野的像风
野的像风 2020-12-04 16:37

I am making a library to get response from a particular URL with specified data and method type. For this, I am making a request with url. But when I set its method type, it

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-04 17:37

    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;

提交回复
热议问题