首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Iphone >

iPhoneHttp通讯

2012-06-29 
iPhoneHttp通信1、准备阶段#import HttpUtil.h@implementation HttpUtil-(NSString *)httpGet:(NSString

iPhoneHttp通信

1、准备阶段

#import "HttpUtil.h"@implementation HttpUtil-(NSString *)httpGet:(NSString *)httpUrl{ NSMutableURLRequest *request =[[NSMutableURLRequest alloc] init]; [request setURL:[NSURL URLWithString:httpUrl]]; [request setHTTPMethod:@"GET"]; // NSString *contentType = [NSString stringWithFormat:@"text/xml"]; NSString *contentType = [NSString stringWithFormat:@"application/x-www-form-urlencoded"];[request addValue:contentType forHTTPHeaderField: @"Content-Type"]; 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]; return result;}-(NSString *)httpPost:(NSString *)httpUrl:(NSString *)postData{ NSMutableURLRequest *request =[[NSMutableURLRequest alloc] init]; [request setURL:[NSURL URLWithString:httpUrl]]; [request setHTTPMethod:@"POST"]; NSString *contentType = [NSString stringWithFormat:@"application/x-www-form-urlencoded"]; [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; NSMutableData *postBody = [NSMutableData data]; [postBody appendData:[[NSString stringWithFormat:postData] dataUsingEncoding:NSUTF8StringEncoding]]; [request setHTTPBody:postBody]; 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]; return result;}@end

?<!--EndFragment-->

热点排行