#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
@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