iphone網路互動json實現

來源:互聯網
上載者:User

/**
 * url:請求地址
 * verb:請求方式
 * 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];


摘自  與時俱進

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.