/**
* Url: request address
* Verb: Request Method
* Parameters: Request parameters
*/
+ (NSString *) sendRequestTo :( NSURL *) url usingVerb :( NSString *) verb withParameters :( NSDictionary *) parameters {
NSString * jsonStr = nil;
NSData * body = nil;
NSMutableString * params = nil;
NSString * contentType = @ "text/html; charset = UTF-8 ";
NSURL * finalURL = url;
If (nil! = Parameters ){
Params = [[NSMutableString alloc] init];
For (id key in parameters ){
NSString * encodedkey = [key stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
CFStringRef value = (CFStringRef) [[parameters objectForKey: key] copy];
CFStringRef encodedValue = CFURLCreateStringByAddingPercentEscapes (kCFAllocatorDefault, value, NULL, (CFStringRef )@";/? : @ & =+ $ ", KCFStringEncodingUTF8 );
[Params appendFormat: @ "% =%@ &", encodedkey, encodedValue];
CFRelease (value );
CFRelease (encodedValue );
}
[Params deleteCharactersInRange: NSMakeRange ([params length]-1, 1)];
}
//
If ([verb isEqualToString: @ "POST"]) {
ContentType = @ "application/x-www-form-urlencoded; charset = UTF-8 ";
Body = [params dataUsingEncoding: NSUTF8StringEncoding];
} Else {
If (nil! = Parameters ){
NSString * urlWithParams = [[url absoluteString] stringByAppendingFormat :@"? % @ ", Params];
FinalURL = [NSURL URLWithString: urlWithParams];
}
}
NSMutableDictionary * headers = [[NSMutableDictionary alloc] init] autorelease];
[Headers setValue: contentType forKey: @ "Content-Type"];
[Headers setValue: @ "text/html" forKey: @ "Accept"];
[Headers setValue: @ "no-cache" forKey: @ "Cache-Control"];
[Headers setValue: @ "no-cache" forKey: @ "Pragma"];
[Headers setValue: @ "close" forKey: @ "Connection"];
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL: finalURL cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 60.0];
[Request setHTTPMethod: verb];
[Request setAllHTTPHeaderFields: headers];
If (nil! = Parameters ){
[Request setHTTPBody: body];
}
[Params release];
//
NSURLResponse * response;
NSError * error = nil;
NSData * responseData = [NSURLConnection sendSynchronousRequest: request returningResponse: & response error: & error];
If (error ){
NSLog (@ "something is wrong: % @", [error description]);
} Else {
If (responseData ){
JsonStr = [[NSString alloc] initWithData: responseData encoding: NSUTF8StringEncoding];
}
}
Return jsonStr;
}
+ (Void) testRequest {
NSLog (@ "test request ...");
NSString * getUrl = @ "http: // 192.168.222.1: 8080/vgirl/isVgirl. php ";
NSString * postUrl = @ "http: // 192.168.222.1: 8080/vgirl/isVgirlByPost. php ";
NSMutableDictionary * dlist = [[NSMutableDictionary alloc] init];
[Dlist setObject: @ "pig" forKey: @ "uid"];
[Dlist setObject: @ "access token" forKey: @ "AccessToken"];
[Dlist setObject: @ "access token secret" forKey: @ "AccessTokenSecret"];
NSString * jsonStrGet = [NetAPI sendRequestTo: [NSURL URLWithString: getUrl] usingVerb: @ "GET" withParameters: dlist];
NSLog (@ "do get: jsonStrGet = % @", jsonStrGet );
NSString * jsonStrPost = [NetAPI sendRequestTo: [NSURL URLWithString: postUrl] usingVerb: @ "POST" withParameters: dlist];
NSLog (@ "do post: jsonStrPost =%@", jsonStrPost );
[Dlist release];
}
Excerpt from advancing with the times