iPad/objective-C同步HTTP请求示例?

发布于 2024-08-30 02:39:00 字数 51 浏览 3 评论 0原文

我只能找到异步 iPad/objective C HTTP 示例。如何进行同步网络请求?

I can only find asynchronous iPad/objective C HTTP examples. How do I do a synchronous web request?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

弃爱 2024-09-06 02:39:00
NSURLRequest * urlRequest = [NSURLRequest requestWithURL:aURL];
NSURLResponse * response = nil;
NSError * error = nil;
NSData * data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
NSURLRequest * urlRequest = [NSURLRequest requestWithURL:aURL];
NSURLResponse * response = nil;
NSError * error = nil;
NSData * data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
为你鎻心 2024-09-06 02:39:00

同意 h4xxr,我会将您转发到

http://allseeing-i.com/ASIHTTPRequest/

是一个很棒的库,具有强大的同步和异步 HTTP 请求方法,并配有代码示例。

Agree with h4xxr and I would forward you to

http://allseeing-i.com/ASIHTTPRequest/

Which is a fantastic lib that has robust HTTP request methods for both synch and asynch complete with code samples.

浪荡不羁 2024-09-06 02:39:00

取决于你想要什么数据。像这样简单的东西是同步的,并且有时很方便:(

NSURL *url = [NSURL URLWithString:@"http://someaddress.asp?somedatarequest=1"];
NSArray *dataArray = [NSArray arrayWithContentsOfURL:url];

字典也存在等价物)

在这种情况下,系统将等待来自 someaddress.asp 的响应 - 因此最好将这样的东西放入后台线程中。

如果您可以控制另一端的数据格式,这可能是一种将数据导入 iPhone/iPad 应用程序的快速而简单的方法...

编辑 - 只是想说明显而易见的事实通常异步通常是最好的!无需等待占用系统资源,特别是如果远程服务器已死亡等...:)

Depends on what data you're after. Something simple like this is synchronous, and is handy from time to time:

NSURL *url = [NSURL URLWithString:@"http://someaddress.asp?somedatarequest=1"];
NSArray *dataArray = [NSArray arrayWithContentsOfURL:url];

(Equivalent also exists for Dictionaries)

In this case, the system will wait for a response from someaddress.asp - therefore best perhaps to put something like this into a background thread.

If you have control over the format of the data at the other end, this can be a quick and easy way to get data into an iPhone/iPad app...

Edit - just wanted to state the obvious that typically asynchronous is usually best! No waiting around tying up system resources, especially if remote server has died etc... :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文