Iphone HTTP请求工具类详解
在项目中我封装了个http请求的工具类,因为项目中只用到post请求,所以get请求的方法只是刚开始试了下,行得通,不知到有没有错误,代码如下:
h文件:
[cpp]
#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文件
[cpp]
#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
摘自 RiverAM的专栏
相关新闻>>
- 发表评论
-
- 最新评论 更多>>