注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

眼睛想旅行

技术就是我的生命与信仰!

 
 
 

日志

 
 
关于我

精通C,C++,python,Erlang。并熟悉各种其他编程语言,用cocos2dx游戏引擎作过几个项目。会MySQL增删改查,了解OpenGL渲染原理。懂单片机,能设计数字电路系统,会画电路图和设计电路板。喜欢了解最新前沿技术,并持续关注和学习新技术。

网易考拉推荐

Objective-C的Http请求(转)  

2014-07-24 23:47:14|  分类: ios学习 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

//prepar request

	NSString *urlString = [NSString stringWithFormat:@"http://urlToSend.com"];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

//set headers
NSString *contentType = [NSString stringWithFormat:@"text/xml"];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

//create the body
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"<xml>"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"<yourcode/>"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"</xml>"] dataUsingEncoding:NSUTF8StringEncoding]];

//post
[request setHTTPBody:postBody];

//get response
NSHTTPURLResponse* urlResponse = nil;
NSError *error = [[NSError alloc] init];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"Response Code: %d", [urlResponse statusCode]);
if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {
NSLog(@"Response: %@", result);

//here you get the response

}
//该代码片段来自于: http://www.sharejs.com/codes/objectc/2526
转自:http://www.sharejs.com/codes/objectc/2526
另外的一个文章中是这样写的:
post请求:

//post数据到地址url

-(NSString*)httpPostDataWithUrl:(NSString*)url postData:(NSString*)data

{

    //使用工厂方法创建

//    NSURLRequest *request = [NSURLRequest requestWithURL:url];

//    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    //同时设置缓存策略和超时时间

    NSMutableURLRequest *[NSURLRequest requestWithURL:[NSURL URLWithString:[_mainUrl stringByAppendingString:url]]  cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:15];

    //设置Http

    NSDictionary *headers = [request allHTTPHeaderFields];

    [headers setValue:@"iOS-Client-ABC" forKey:@"User-Agent"];

    //设置请求方法

    //[request setHTTPMethod:@"GET"];

    [request setHTTPMethod:@"POST"];

    //设置要发送的正文内容(适用于Post请求)

    //NSString *content = @"username=stanyung&password=123";

    NSData *contentData = [data dataUsingEncoding:NSUTF8StringEncoding];

    [request setHTTPBody:contentData];

    //同步执行Http请求,获取返回数据

    NSURLResponse *response;

    NSError *error = nil;

    NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    //返数据转成字符串

    NSString *html = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];

    NSLog(@"get html:%@",html);

    //(如果有错误)错误描述

    if (error) {

        NSString *errorDesc = [error localizedDescription];

        NSLog(@"%@",errorDesc);

    }

    

    //获取状态码和HTTP响应头信息

    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;

    NSInteger statusCode = [httpResponse statusCode];

    NSLog(@"statusCode:%i",statusCode);

    NSDictionary *responseHeaders = [httpResponse allHeaderFields];

    NSString *cookie = [responseHeaders valueForKey:@"Set-Cookie"];

    NSLog(@"cookie:%@",cookie);

    return html;

}

get请求

-(NSString*)httpGetDataWithUrl:(NSString*)url

{

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[_mainUrl stringByAppendingString:url]]];

    NSDictionary *headers = [request allHTTPHeaderFields];

    [headers setValue:@"iOS-Client-ABC" forKey:@"User-Agent"];

    

    //同步执行Http请求,获取返回数据

    NSURLResponse *response;

    NSError *error = nil;

    NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    //返数据转成字符串

    NSString *html = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];

    NSLog(@"get html:%@",html);

    //(如果有错误)错误描述

    if (error) {

        NSString *errorDesc = [error localizedDescription];

        NSLog(@"%@",errorDesc);

    }

    

    //获取状态码和HTTP响应头信息

    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;

    NSInteger statusCode = [httpResponse statusCode];

    NSLog(@"statusCode:%i",statusCode);

    NSDictionary *responseHeaders = [httpResponse allHeaderFields];

    NSString *cookie = [responseHeaders valueForKey:@"Set-Cookie"];

    NSLog(@"cookie:%@",cookie);

    return html;

}



  评论这张
 
阅读(460)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017