在iOS 5上使用libcurl作为NSURLConnection的替代方案 [英] Using libcurl on iOS 5 as an alternative to NSURLConnection

查看:158
本文介绍了在iOS 5上使用libcurl作为NSURLConnection的替代方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:NSURLConnection现在似乎正确支持100-Continue。在任何情况下,此答案都包含指向为iOS / OSX构建libcurl的脚本的链接。

Update: NSURLConnection now seems to properly support 100-Continue. In any case, this answer contains a link to the script to build libcurl for iOS/OSX.

我在 NSURLConnection 上遇到了一些困难,因为它不支持 RFC 2616的第8.2.3节(HTTP / 1.1)。

I'm having a bit of a hard time with NSURLConnection, given that it doesn't support Section 8.2.3 of RFC 2616 (HTTP/1.1).

基本上客户需要能够支持发送标题 Expect:100-Continue ;在发送请求标头后,必须等待来自服务器的响应,状态代码 100 ,然后再发送 POST / PUT body。

Basically the client needs to be able to support sending the header Expect: 100-Continue; after sending the request headers, it must wait for a response from the server with the status code 100 before sending the POST/PUT body.

此外, NSURLConnection (因此所有构建在它之上的库)在上传所有数据之前都不会从服务器返回任何响应 - 这很痛苦,因为服务器可能会立即拒绝上传(凭证无效,没有空间,文件太大等)。
虽然它对小文件工作(内容完全上传,并且调用了响应的委托方法),但是在大文件上而不是从服务器获取错误响应(我肯定确定已发送),我刚刚收到连接失败错误。

Furthermore, NSURLConnection (and consequently all libs that build on top of it) won't return any response from the server until all data is uploaded - which is a pain since the server may reject the upload right away (invalid credentials, no space, file too large, etc). While it does "work" for small files (content is fully uploaded and delegate method with response is called), on large files instead of getting the error response from the server (which I am positively sure is sent), I just get a "Connection failed" error.

我知道 libcurl 正确支持 100-Continue 规范所以我需要一些帮助来编译它并在iOS 5项目上运行它。

I know libcurl properly supports the 100-Continue spec so I need some help compiling it and getting it up and running on a iOS 5 project.

我开始使用这篇文章(滚动到底部)但我无法走远...

I started with this post(scroll to bottom) but I couldn't get far...

进行了这些更改......

Made these changes...

export CC=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/llvm-gcc-4.2
export CFLAGS="-arch armv7 --sysroot=/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk"
export CPP=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/llvm-cpp-4.2
export AR=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/ar
./configure --disable-shared --without-ssl --without-libssh2 --without-ca-bundle --without-ldap --disable-ldap --host=arm-apple-darwin10 --build=arm-apple-darwin10
make clean
make
ar rv libcurl.armv7.a lib/*.o

...但编译失败并显示消息

... but compilation fails with message

(...)
checking for arm-apple-darwin10-gcc... /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/llvm-gcc-4.2
checking whether the C compiler works... no
configure: error: in `/Users/bruno/Downloads/curl-7.21.4':
configure: error: C compiler cannot create executables

我'使用curl 7.21.4,从Apple的开源网站下载。

I'm using curl 7.21.4, downloaded from Apple's open source site.

那么,如何编译curl并在iOS 5项目中使用它,优先考虑是否支持SSL?

So, how can I compile curl and use it on a iOS 5 project, preferably with SSL support?

推荐答案

这对我来说最新的SDK:

This worked for me with the latest SDK:

#!/bin/sh 
export SDK=5.0

buildit()
{
    target=$1
    platform=$2

    export CC=/Developer/Platforms/${platform}.platform/Developer/usr/bin/gcc
    export CFLAGS="-arch ${target} -isysroot /Developer/Platforms/${platform}.platform/Developer/SDKs/${platform}${SDK}.sdk"
    export CPP="/Developer/Platforms/${platform}.platform/Developer/usr/bin/llvm-cpp-4.2"
    export AR=/Developer/Platforms/${platform}.platform/Developer/usr/bin/ar
    export RANLIB=/Developer/Platforms/${platform}.platform/Developer/usr/bin/ranlib

    ./configure --disable-shared --without-ssl --without-libssh2 --without-ca-bundle --without-ldap --disable-ldap \
            --host=${target}-apple-darwin10

    make clean
    make
    $AR rv libcurl.${target}.a lib/*.o
}

buildit armv6 iPhoneOS
buildit armv7 iPhoneOS
buildit i386 iPhoneSimulator

lipo -create libcurl.armv7.a libcurl.armv6.a libcurl.i386.a -output libcurl.a

这篇关于在iOS 5上使用libcurl作为NSURLConnection的替代方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆