Objective-C的Http请求(转)

//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;

}



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值