Iphone HTTP请求工具类
来源:互联网 发布:晋业通软件怎样使用 编辑:程序博客网 时间:2024/05/18 17:41
在项目中我封装了个http请求的工具类,因为项目中只用到post请求,所以get请求的方法只是刚开始试了下,行得通,不知到有没有错误,代码如下:
h文件:
#import <Foundation/Foundation.h>/* *自定义http代理 */@protocol MyhttpUitlDelegate <NSObject>-(void) setServicereturnData:(NSString*)servicedata;@end@interface MyHTTPUtil : NSObject<NSXMLParserDelegate> { NSMutableData *webData;//异步请求返回回来的数据 id<MyhttpUitlDelegate> myhttputildelegate; NSString *dialogcanshow;//防止快速两次请求以上弹出多个等待对话筐 //等待对话筐 UIAlertView *mt;}@property(nonatomic,retain)id<MyhttpUitlDelegate> myhttputildelegate;@property(nonatomic,retain)UIAlertView *mt;@property(nonatomic,retain) NSString *dialogcanshow;//防止快速两次请求以上弹出多个等待对话筐+(NSString *)httpForTongbuGET:(NSString *)urlstring;+(NSString *)httpForTongbuPOST:(NSString *)urlstring addParms:(NSData *)urlparms;//-(void)httpForYibuPOST:(NSString *)urlstring addParms:(NSString *)urlparms;-(void)httpForYibuPOSTForData:(NSString *)urlstring addParms:(NSData *)urlparms;-(void)httpForYibuGET:(NSString *)urlstring addParms:(NSString *)urlparms;-(void)httpForYibuPOSTPageparms:(NSString *)urlstring addParms:(NSData *)pageparms;-(void)showwaitalertview;@end
m文件
#import "MyHTTPUtil.h"#import "FileUtils.h"@implementation MyHTTPUtil//url公用部分//NSString *baseurl = @"http://192.168.0.63:8080/mbtravel/";//NSString *baseurl = @"http://192.168.0.63:8081/mbtr/";NSString *baseurl = @"http://192.168.128.1:8080/mbtravel/";NSInteger PagingRows = 5;//每页的行数@synthesize myhttputildelegate;@synthesize dialogcanshow;@synthesize mt;-(void)dealloc{[myhttputildelegate release]; [dialogcanshow release]; [mt release];[super dealloc];}-(id)init{ self.dialogcanshow = @"false"; return [super init];}//http的同步get请求,返回服务器返回的数据+(NSString *)httpForTongbuGET:(NSString *)urlstring{ NSString *allurl = [NSString stringWithFormat:@"%@%@",baseurl,urlstring]; NSURL* url = [NSURL URLWithString:allurl]; NSMutableURLRequest* request = [[[NSMutableURLRequest alloc]init]autorelease]; [request setURL:url]; [request setHTTPMethod:@"GET"]; NSString *contentType = [NSString stringWithFormat:@"text/xml"];//@"application/json" [request addValue:contentType forHTTPHeaderField:@"Content-Type"]; NSHTTPURLResponse *urlResponese = nil; NSError *error = [[NSError alloc]init]; NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponese error:&error]; NSString* result= [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; if([urlResponese statusCode] >=200&&[urlResponese statusCode]<300){ return result; } return nil; [error release]; [result release];}//异步get请求-(void)httpForYibuGET:(NSString *)urlstring addParms:(NSString *)urlparms{ // if ([ValueUtils caseEquals:self.dialogcanshow to:@"true"]){ // [self showwaitalertview];//弹出等待对话筐 // } // NSString *allurl = [NSString stringWithFormat:@"%@%@%@",baseurl,urlstring,urlparms]; NSLog(@"%@",allurl); NSURL *url = [NSURL URLWithString:allurl]; NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:15]; [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; //[theRequest setTimeoutInterval:15];//设置了也是无效 [theRequest setHTTPMethod:@"GET"]; NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES]; if(theConnection) { webData = [[NSMutableData data] retain]; }}//////异步post请求,带参数////-(void)httpForYibuPOST:(NSString *)urlstring addParms:(NSString *)urlparms{//// if ([ValueUtils caseEquals:self.dialogcanshow to:@"true"]) {//// [self showwaitalertview];//弹出等待对话筐//// }// // NSString *allurl = [NSString stringWithFormat:@"%@%@%@",baseurl,urlstring,urlparms];// //NSStringEncoding enc=CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000); //转换编码,包括中文// // NSString *allurl2=[allurl stringByAddingPercentEscapesUsingEncoding:enc];// NSString *allurl2= [allurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];// NSLog(@"url===%@",allurl2);// NSURL *url = [NSURL URLWithString:allurl2]; // NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:15]; // [theRequest setValue:@"zh-cn" forHTTPHeaderField:@"Accept-Language"];// [theRequest setValue:@"gzip, deflate" forHTTPHeaderField:@"Accept-Encoding"];// [theRequest addValue: @"application/x-www-form-urlencoded;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];// //[theRequest setTimeoutInterval:15];//设置了也是无效// [theRequest setHTTPMethod:@"POST"]; // NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];// // // if(theConnection) {// webData = [[NSMutableData data] retain];// // }//}//基本上只用下面这三个方法//http的同步post请求,返回服务器返回的数据+(NSString *)httpForTongbuPOST:(NSString *)urlstring addParms:(NSData *)urlparms{ NSString *allurl = [NSString stringWithFormat:@"%@%@",baseurl,urlstring]; NSURL* url = [NSURL URLWithString:allurl]; NSMutableURLRequest* request = [[[NSMutableURLRequest alloc]init]autorelease]; [request setURL:url]; [request setHTTPMethod:@"POST"]; [request setValue:@"zh-cn" forHTTPHeaderField:@"Accept-Language"]; [request setValue:@"gzip, deflate" forHTTPHeaderField:@"Accept-Encoding"]; [request addValue: @"application/x-www-form-urlencoded;charset=UTF-8" forHTTPHeaderField:@"Content-Type"]; // [request addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];//写这句而不是上面那句,就会出错 [request setHTTPBody:urlparms]; NSHTTPURLResponse *urlResponese = nil; NSError *error = [[NSError alloc]init]; NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponese error:&error]; NSString* result= [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; if([urlResponese statusCode] >=200&&[urlResponese statusCode]<300){ return result; } return nil; [error release]; [result release];}//异步post请求,后面参数是nsdata类型,主要针对参数提交-(void)httpForYibuPOSTForData:(NSString *)urlstring addParms:(NSData *)urlparms{ NSString *allurl = [NSString stringWithFormat:@"%@%@",baseurl,urlstring]; NSString *allurl2= [allurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL *url = [NSURL URLWithString:allurl2]; NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:15]; [theRequest setValue:@"zh-cn" forHTTPHeaderField:@"Accept-Language"]; [theRequest setValue:@"gzip, deflate" forHTTPHeaderField:@"Accept-Encoding"]; [theRequest addValue: @"application/x-www-form-urlencoded;charset=UTF-8" forHTTPHeaderField:@"Content-Type"]; [theRequest setHTTPMethod:@"POST"]; [theRequest setValue:[NSString stringWithFormat:@"%d",PagingRows] forHTTPHeaderField:@"PagingRows"]; [theRequest setHTTPBody:urlparms]; if ([ValueUtils caseEquals:self.dialogcanshow to:@"true"]){ [self showwaitalertview];//弹出等待对话筐 } NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES]; if(theConnection) { webData = [[NSMutableData data] retain]; }}//异步post请求,主要针对分页查询-(void)httpForYibuPOSTPageparms:(NSString *)urlstring addParms:(NSData *)pageparms{ // if ([ValueUtils caseEquals:self.dialogcanshow to:@"true"]) { // [self showwaitalertview];//弹出等待对话筐 // } NSString *allurl = [NSString stringWithFormat:@"%@%@",baseurl,urlstring]; NSString *allurl2= [allurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL *url = [NSURL URLWithString:allurl2]; NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:15]; [theRequest setValue:@"zh-cn" forHTTPHeaderField:@"Accept-Language"]; [theRequest setValue:@"gzip, deflate" forHTTPHeaderField:@"Accept-Encoding"]; [theRequest addValue: @"application/x-www-form-urlencoded;charset=UTF-8" forHTTPHeaderField:@"Content-Type"]; [theRequest setHTTPMethod:@"POST"]; [theRequest setValue:[NSString stringWithFormat:@"%d",PagingRows] forHTTPHeaderField:@"PagingRows"]; [theRequest setHTTPBody:pageparms]; if ([ValueUtils caseEquals:self.dialogcanshow to:@"true"]){ [self showwaitalertview];//弹出等待对话筐 } NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES]; if(theConnection) { webData = [[NSMutableData data] retain]; }}- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {[webData setLength:0];// if ([ValueUtils caseEquals:self.dialogcanshow to:@"true"]){// [self showwaitalertview];//弹出等待对话筐// } }- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {[webData appendData:data];}- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { if (mt!=nil) { [mt dismissWithClickedButtonIndex:1 animated:YES]; } [self performSelectorInBackground:@selector(showerroralertview) withObject:nil]; //[mt dismissWithClickedButtonIndex:1 animated:YES];}- (void)connectionDidFinishLoading:(NSURLConnection *)connection { if (mt!=nil) { [mt dismissWithClickedButtonIndex:1 animated:YES]; }[connection release]; NSString* result= [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding]; [self.myhttputildelegate setServicereturnData:result];//为页面设值[webData release]; [result release];}//弹出error对话筐-(void)showerroralertview{ UIAlertView *erroralert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请求数据失败" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil]; [erroralert show]; [erroralert release]; }//弹出等待对话筐-(void)showwaitalertview{ mt = [[UIAlertView alloc]initWithTitle:nil message:@"正在加载......" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil]; [mt show]; }@end
- Iphone HTTP请求工具类
- Iphone HTTP请求工具类
- Http请求工具类
- http请求工具类
- http请求工具类
- HTTP 请求工具类
- http请求工具类
- HTTP请求工具类
- HTTP请求工具类
- http请求工具类
- http请求工具类
- 工具类【Http请求】
- Http请求工具类
- HTTP 请求 工具类
- Http请求工具类
- Http请求工具类
- HTTP请求工具类
- Android----http请求工具类
- 借记得么
- <cocos2d-x for wp7>使用cocos2d-x和BOX2D来制作一个BreakOut(打砖块)游戏(一)
- 传微软开始测试家庭自动化系统
- [转载]基于arm+uClinux的嵌入式系统的开发
- Sicily 4189 统计数字(难得水题)
- Iphone HTTP请求工具类
- 中文版Dreamweaver CS4菜单栏变成英文问题
- 关于增量模型和迭代模型的区别
- MFC OLEDB数据库
- OLEDB修改
- 斗地主算法之发牌,洗牌
- arm-linux-gcc和arm-elf-gcc 区别 【转】
- OLEDB添加
- OLEDB删除