当前位置:编程学习 > wap >>

Iphone HTTP请求工具类详解

在项目中我封装了个http请求的工具类,因为项目中只用到post请求,所以get请求的方法只是刚开始试了下,行得通,不知到有没有错误,代码如下:
h文件:
[cpp]
#import <Foundation/Foundation.h> 
 
/*
 *自定义http代理
 */ 
@protocol MyhttpUitlDelegate <NSObject> 
-(void) setServicereturnData:(NSString*)servicedata; 
@end 
 
@inte易做图ce 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"]; 
//      [th

补充:移动开发 , IOS ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,