发送Http请求(POST GET)的方法

来源:代码人生(http://aminby.net/2010/07/iphone-develop-how-to-send-http-request/)

我们知道Http有Get和Post两种方法,我们分开说吧.

另注: 今天讲的方法是同步的请求, 异步的方法我还没试过, 不知道有没有使用异步的需求, 有的话于发上来和大家分享.

1.Get方法
1.1 使用NSMutableURLRequest
view plaincopy to clipboardprint?
NSURL* url = [NSURL URLWithString:@"http://aminby.net"];     
NSMutableURLRequest* request = [NSMutableURLRequest new];     
[request setURL:url];     
[request setHTTPMethod:@"GET"];     
NSHTTPURLRequest* response;     
NSData* data = [NSURLConnection sendSynchronousRequest:request     
              returningResponse:&response error:nil];     
[NSString* strRet = [[NSString alloc] initWithData:data encoding:NSUTF8String];     
NSLog(strRet);     
[strRet release];    
NSURL* url = [NSURL URLWithString:@"http://aminby.net"];  
NSMutableURLRequest* request = [NSMutableURLRequest new];  
[request setURL:url];  
[request setHTTPMethod:@"GET"];  
NSHTTPURLRequest* response;  
NSData* data = [NSURLConnection sendSynchronousRequest:request  
              returningResponse:&response error:nil];  
[NSString* strRet = [[NSString alloc] initWithData:data encoding:NSUTF8String];  
NSLog(strRet);  
[strRet release];   

1.2 使用NSString

view plaincopy to clipboardprint?
/ options有两个枚举,NSMappedRead这个不懂, NSUncachedRead是不缓存     
[NSData dataWithContentsOfURL:(NSURL *)url     
        options:(NSUInteger)readOptionsMask     
        error:(NSError **)errorPtr]     
//  或者     
[NSData dataWithContentsOfURL:(NSURL *)url];    
/ options有两个枚举,NSMappedRead这个不懂, NSUncachedRead是不缓存  
[NSData dataWithContentsOfURL:(NSURL *)url  
        options:(NSUInteger)readOptionsMask  
        error:(NSError **)errorPtr]  
//  或者  
[NSData dataWithContentsOfURL:(NSURL *)url];   

1.2和1.3的方法是缺点是没办法知道response的status,一般是返回200-299之间的数值代表请求成功.我们可以依照这个code来做数据处理, 如果对地址存在很有把握,就可以使用后两种简单的GET方法.

今天查了一下手册,发现NSArray NSDictionary 也有xxxxWithContentsOfURL的方法, 这两个我还没用过, 应该是跟NSData和NSString一样,但具体怎么用我还不清楚.

2.Post方法
2.1 使用NSMutableURLRequest
view plaincopy to clipboardprint?
NSURL* url = [NSURL URLWithString:@"http://aminby.net"];     
NSMutableURLRequest*  request = [NSMutableURLRequest new];     
[request setURL:url];     
[request  setHTTPMethod:@"GET"];     
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];     
[request setHTTPBody:@"some param"];     
NSHTTPURLRequest* response;     
NSData* data =  [NSURLConnection sendSynchronousRequest:request     
         returningResponse:&response error:nil];     
[NSString* strRet =  [[NSString alloc] initWithData:data encoding:NSUTF8String];     
NSLog(strRet);     
[strRet  release];    
DECLARE @Object int; DECLARE @HR int; DECLARE @Property nvarchar(255); DECLARE @Return nvarchar(255); DECLARE @Source nvarchar(255), @Desc nvarchar(255); DECLARE @httpStatus int; DECLARE @response varchar(8000); --创建 OLE 对象的实例 EXEC @HR = sp_OACreate N'MSXML2.XMLHTTP.6.0',@Object OUT; IF @HR 0 BEGIN EXEC sp_OAGetErrorInfo @Object,@Source OUT,@Desc OUT; RAISERROR('Error Creating COM Component 0x%x, %s, %s',16,1, @HR, @Source, @Desc) GOTO END_ROUTINE END BEGIN --Open EXEC @HR = sp_OAMethod @Object,N'open',Null,'GET','http://localhost:1728/HttpServer/submit.aspx',FALSE; IF @HR 0 BEGIN EXEC sp_OAGetErrorInfo @Object,@Source OUT,@Desc OUT; RAISERROR('Open 0x%x, %s, %s',16,1, @HR, @Source, @Desc) GOTO CLEANUP END --setRequestHeader EXEC @HR = sp_OAMethod @Object,N'setRequestHeader',Null,'Content-Type','text/xml'; IF @HR 0 BEGIN EXEC sp_OAGetErrorInfo @Object,@Source OUT,@Desc OUT; RAISERROR('setRequestHeader 0x%x, %s, %s',16,1, @HR, @Source, @Desc) GOTO CLEANUP END --send EXEC @HR = sp_OAMethod @Object,N'send',Null,''; IF @HR 0 BEGIN EXEC sp_OAGetErrorInfo @Object,@Source OUT,@Desc OUT; RAISERROR('send 0x%x, %s, %s',16,1, @HR, @Source, @Desc) GOTO CLEANUP END --readyState EXEC @HR = sp_OAGetProperty @Object,'readyState', @httpStatus OUT; IF @HR 0 BEGIN EXEC sp_OAGetErrorInfo @Object,@Source OUT,@Desc OUT; RAISERROR('readyState 0x%x, %s, %s',16,1, @HR, @Source, @Desc) GOTO CLEANUP END --verify status IF @httpStatus 4 BEGIN RAISERROR('readyState http status bad', 16,1) GOTO CLEANUP END --status EXEC @HR = sp_OAGetProperty @Object,'status', @httpStatus OUT; IF @HR 0 BEGIN EXEC sp_OAGetErrorInfo @Object,@Source OUT,@Desc OUT; RAISERROR('getstatus 0x%x, %s, %s',16,1, @HR, @Source, @Desc) GOTO CLEANUP END --verify status IF @httpStatus 200 BEGIN Print Cast(@httpStatus As varchar) RAISERROR('Open http status bad', 16,1) GOTO CLEANUP END --responseText EXEC @HR = sp_OAGetProperty @Object, 'responseText', @response OUT IF @HR 0 BEGIN EXEC sp_OAGetErrorInfo @Object,@Source OUT,@Desc OUT; RAISERROR('responseText 0x%x, %s, %s',16,1, @HR, @Source, @Desc) GOTO CLEANUP END Print @response END CLEANUP: BEGIN EXEC @HR = sp_OADestroy @Object; IF @HR 0 BEGIN EXEC sp_OAGetErrorInfo @Object,@Source OUT,@Desc OUT; SELECT HR = convert(varbinary(4),@HR),Source=@Source,Description=@Desc; END END END_ROUTINE: RETURN; GO
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值