In the project, I encapsulated an http request tool class. Because only post requests are used in the project, the get request method is only available at the beginning, the Code is as follows:
H file:
[Cpp]
# Import <Foundation/Foundation. h>
/*
* Custom http Proxy
*/
@ Protocol MyhttpUitlDelegate <NSObject>
-(Void) setServicereturnData :( NSString *) servicedata;
@ End
@ Interface MyHTTPUtil: NSObject <NSXMLParserDelegate> {
NSMutableData * webData; // The data returned by the asynchronous request
Id <MyhttpUitlDelegate> myhttputildelegate;
NSString * dialogcanshow; // prevents more than two requests from popping up.
// Wait for the conversation basket
UIAlertView * mt;
}
@ Property (nonatomic, retain) id <MyhttpUitlDelegate> myhttputildelegate;
@ Property (nonatomic, retain) UIAlertView * mt;
@ Property (nonatomic, retain) NSString * dialogcanshow; // prevents more than two requests from being sent quickly.
+ (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 file
[Cpp]
# Import "MyHTTPUtil. h"
# Import "FileUtils. h"
@ Implementation MyHTTPUtil
// Url public part
// 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; // number of rows per page
@ Synthesize myhttputildelegate;
@ Synthesize dialogcanshow;
@ Synthesize mt;
-(Void) dealloc {
[Myhttputildelegate release];
[Dialogcanshow release];
[Mt release];
[Super dealloc];
}
-(Id) init {
Self. dialogcanshow = @ "false ";
Return [super init];
}
// The http synchronous get request returns the data returned by the server
+ (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];
}
// Asynchronous get request
-(Void) httpForYibuGET :( NSString *) urlstring addParms :( NSString *) urlparms {
// If ([ValueUtils caseEquals: self. dialogcanshow to: @ "true"]) {
// [Self showwaitalertview]; // wait for the dialog box to pop up
//}
//
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]; // The setting is also invalid.
[TheRequest setHTTPMethod: @ "GET"];
NSURLConnection * theConnection = [[NSURLConnection alloc] initWithRequest: theRequest delegate: self startImmediately: YES];
If (theConnection ){
WebData = [[NSMutableData data] retain];
}
}
//
/// Asynchronous post request with Parameters
//
//-(Void) httpForYibuPOST :( NSString *) urlstring addParms :( NSString *) urlparms {
/// If ([ValueUtils caseEquals: self. dialogcanshow to: @ "true"]) {
/// [Self showwaitalertview]; // wait for the dialog box to pop up
////}
//
// NSString * allurl = [NSString stringWithFormat: @ "% @", baseurl, urlstring, urlparms];
/// NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_18030_2000); // conversion encoding, including Chinese Characters
/// 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]; // The setting is also invalid.
// [TheRequest setHTTPMethod: @ "POST"];
// NSURLConnection * theConnection = [[NSURLConnection alloc] initWithRequest: theRequest delegate: self startImmediately: YES];
//
//
// If (theConnection ){
// WebData = [[NSMutableData data] retain];
//
//}
//}
// Basically only use the following three methods
// The http synchronous post request returns the data returned by the server
+ (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"]; // if you write this sentence instead of the above sentence, an error will occur.
[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];
}
// Asynchronous post request. The following parameters are of the nsdata type and are mainly submitted for parameters.
-(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]; // wait for the dialog box to pop up
}
NSURLConnection * theConnection = [[NSURLConnection alloc] initWithRequest: theRequest delegate: self startImmediately: YES];
If (theConnection ){
WebData = [[NSMutableData data] retain];
}
}
// Asynchronous post request, mainly for paging Query
-(Void) httpForYibuPOSTPageparms :( NSString *) urlstring addParms :( NSData *) pageparms {
// If ([ValueUtils caseEquals: self. dialogcanshow to: @ "true"]) {
// [Self showwaitalertview]; // wait for the dialog box to pop up
//}
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]; // wait for the dialog box to pop up
}
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]; // wait for the dialog box to pop up
//}
}
-(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 defined mselectorinbackground: @ 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]; // set the page Value
[WebData release];
[Result release];
}
// Pop-up error dialog box
-(Void) showerroralertview {
UIAlertView * erroralert = [[UIAlertView alloc] initWithTitle: @ "prompt" message: @ "request data failed" delegate: self cancelButtonTitle: nil otherButtonTitles: @ "OK", nil];
[Erroralert show];
[Erroralert release];
}
// Wait for the dialog box to pop up
-(Void) showwaitalertview {
Mt = [[UIAlertView alloc] initWithTitle: nil message: @ "loading..." delegate: self cancelButtonTitle: nil otherButtonTitles: nil, nil];
[Mt show];
}
@ End
From RiverAM's column