gpt4 book ai didi

iphone - iPhone 中的 Http 请求/响应 API

转载 作者:行者123 更新时间:2023-11-29 13:30:35 24 4
gpt4 key购买 nike

在 iPhone 应用程序中,是否所有网络操作都仅使用 NSURLConnections 完成??我正在使用 NSURLConnections 及其委托(delegate)方法 ConnectiondidReceiveResponseConnectiondidFinishLoading 等来下载文件。

我的问题是 NSURLConnection API 是否仅在 cocoa-touch 中可用于 Http 请求/响应?还有其他可用的 API 吗??

我们也可以使用 NSURLConnection 来上传文件吗??

我想有很多问题,但我真的很困惑。请帮帮我!

最佳答案

我也觉得用 NSURLConnection 上传文件很困难。最后,我使用了 AFNetworking 库。效果很好。

NSURL *url = [NSURL URLWithString:@"http://api-base-url.com"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"avatar.jpg"], 0.5);
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/upload" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
[formData appendPartWithFileData:imageData name:@"avatar" fileName:@"avatar.jpg" mimeType:@"image/jpeg"];
}];

AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:request] autorelease];
[operation start];

您可以在以下地址找到图书馆页面。

https://github.com/AFNetworking/AFNetworking

此外,还有管理 Objective-C 库的 CocoaPods。我正在尝试使用它。

https://github.com/CocoaPods/CocoaPods/

关于iphone - iPhone 中的 Http 请求/响应 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12003970/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com