URLConnection

#import <Foundation/Foundation.h>


@interface URLRequest : NSObject {
    id _delegate;    
    SEL _action;
    NSString *_xml;
    NSMutableData *responseData;
    NSMutableURLRequest *request;
    NSURLConnection *conn;
    NSTimer    *timer;
    NSString *autoStr;
}

@property (assign) id delegate;
@property (assign) SEL action;
@property (nonatomic,retain) NSString *xml;
@property (nonatomic,retain) NSMutableData *responseData;
@property (nonatomic,retain) NSMutableURLRequest *request;
@property (nonatomic,retain) NSString *autoStr;

-(id)init:(id) delegate xmlData:(NSString *)xml action:(SEL)action;
-(void)send;
@end


//
//  URLRequest.m
//  ibaby_pro
//
//  Created by wgd on 12-3-14.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "URLRequest.h"
#import "iChatDefine.h"
#import "NetRstModel.h"
#import "GlobalData.h"

@implementation URLRequest

@synthesize  delegate = _delegate,xml = _xml ,responseData,action = _action,request,autoStr;

-(id)init:(id) delegate xmlData:(NSString *)xml action:(SEL)action{    
    if(self = [super init])
    {
        self.delegate = delegate;
        self.xml = xml;
        self.action = action;
        return self;
    }    
    return nil;
}

-(void)send{
    
    autoStr = [NSString stringWithFormat:@"abc"];
    [autoStr retain];
    NSLog(@"[autoStr retainCount]:%d",[autoStr retainCount]);
    
    NSString* str = [NSString stringWithFormat:@"http://%@:%d/%@", IP_SERVER, PORT_SERVER,SERVLET];

    self.request = [[[NSMutableURLRequest alloc] init] autorelease];

    //self.request = [[NSMutableURLRequest alloc] init];
        NSLog(@"[request retainCount]:%d",[request retainCount]);
    GlobalData *globalData = [GlobalData shareGlobalData];
    globalData.globalRequest  = self.request;
    //request = [[NSMutableURLRequest alloc] init] ;
    [request setURL:[NSURL URLWithString:str]];
    [request setHTTPMethod:@"POST"];   
    [request addValue:@"applicatioin/bin;charset=UTF-8" forHTTPHeaderField: @"Content-Type"];
    NSMutableData *postBody = [NSMutableData data];  
    NSLog(@"send xml:%@",_xml);
    [postBody appendData:[NSMutableData dataWithBytes:[_xml UTF8String]
                                               length:strlen([_xml UTF8String])]];
    [request setHTTPBody:postBody];
     conn = [[NSURLConnection alloc] init];    
    NSLog(@"[request retainCount]:%d",[request retainCount]);
    [conn initWithRequest:request delegate:self];
    NSLog(@"[request retainCount]:%d",[request retainCount]);
    timer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(timeOut) userInfo:nil repeats:NO];
        NSLog(@"[request retainCount]:%d",[request retainCount]);
}
    
-(void)timeOut{
    [conn cancel];
    [conn release];
    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"警告" message:@"服务超时" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];
    
}

-(void)dealloc{
    [_xml release];
    [responseData release];

    NSLog(@"[request retainCount]:%d",[request retainCount]);
        [request release];
    //[request release];
        [super dealloc];
}



#pragma mark -
#pragma mark connection received data

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {    
    [responseData appendData:data];    
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {    
    self.responseData = [NSMutableData data];
}

- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
    
    NSLog(@"[autoStr retainCount]:%d",[autoStr retainCount]);
    GlobalData *glbalData = [GlobalData shareGlobalData];
    
    NSLog(@"global request:%d",[glbalData.globalRequest retainCount] );
    [timer invalidate];
    NSString *aString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    NSLog(@"Receive String:%@",aString);
    if([_delegate respondsToSelector:_action])
    {
        [_delegate performSelector:_action withObject:aString];
    }
    [aString release];
    [conn release];
    //GlobalData *glbalData = [GlobalData shareGlobalData];
    
    NSLog(@"global request:%d",[glbalData.globalRequest retainCount] );
    
}

- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{    [timer invalidate];
    NSLog(@"fail,error:%@",error);
    NetBaseRst *errorRst = [[[NetBaseRst alloc] init] autorelease];
    errorRst.returnCode = [error code];
    errorRst.message = [error localizedDescription];
    if(-1004 ==[error code])
    {    
            UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"警告" message:@"网络故障" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
            [alert release];
            [conn release];
            return;
    }
    if([_delegate respondsToSelector:_action])
    {
        [_delegate performSelector:_action withObject:errorRst];
        return;
    }
        return;
}


@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值