Iphone network interaction json implementation

Source: Internet
Author: User

/**
* 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.