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:@""] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@""] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"
"] 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;

}



你可能感兴趣的:(ios学习)