iOS 中的 Android okhttp 等价于什么?
What is the equivalent of Android okhttp in iOS?
Android 来源
String url = "http://www.example.com";
OkHttpClient client = clientBuilder.build();
Request request = new Request.Builder()
.url(url)
.addHeader("Host", "api.example.com")
.addHeader("Referer", "http://www.examplegood.com/")
.build();
Response response = client.newCall(request).execute();
效果很好。我在 iOS 中尝试使用 NSURLConnection 请求。
iOS 来源
NSString *url = @"http://www.example.com";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setValue:@"api.example.com" forHTTPHeaderField:@"Host"];
[request setValue:@"http://www.examplegood.com/" forHTTPHeaderField:@"Referer"];
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod: @"GET"];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSMutableData *result = [[NSMutableData alloc] init];
[result appendData:data];
NSString* resultStr = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
NSLog(@"result:%@",resultStr);
}];
效果不好。没有回应。
iOS 中 Android OkHttpClient newCall() 的等价物是什么?
我使用 NSURLConnection 解决了这个问题。并通过该委托获取响应数据。
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
Android 来源
String url = "http://www.example.com";
OkHttpClient client = clientBuilder.build();
Request request = new Request.Builder()
.url(url)
.addHeader("Host", "api.example.com")
.addHeader("Referer", "http://www.examplegood.com/")
.build();
Response response = client.newCall(request).execute();
效果很好。我在 iOS 中尝试使用 NSURLConnection 请求。
iOS 来源
NSString *url = @"http://www.example.com";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setValue:@"api.example.com" forHTTPHeaderField:@"Host"];
[request setValue:@"http://www.examplegood.com/" forHTTPHeaderField:@"Referer"];
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod: @"GET"];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSMutableData *result = [[NSMutableData alloc] init];
[result appendData:data];
NSString* resultStr = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
NSLog(@"result:%@",resultStr);
}];
效果不好。没有回应。 iOS 中 Android OkHttpClient newCall() 的等价物是什么?
我使用 NSURLConnection 解决了这个问题。并通过该委托获取响应数据。
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response