iOS URL缩短

@interface ViewController ()

@property (nonatomic,retain)NSMutableData *data;

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    NSMutableURLRequest *request = [NSMutableURLRequest new];

    [request setURL:[NSURL URLWithString:@"http://dwz.cn/create.php"]];

    //    [request setURL:[NSURL URLWithString:@"http://dwz.cn/query.php"]];

    [request setHTTPMethod:@"POST"];

    NSMutableData *body = [NSMutableData data];

    //

    NSString *urlStr = @"http://help.baidu.com/question?prod_en=webmaster&class=123&id=1000913";

    NSString *outputStr = (NSString *)

    CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,

                                            (CFStringRef)urlStr,

                                            NULL,

                                            (CFStringRef)@"!*'();:@&=+$,/?%#[]",

                                            kCFStringEncodingUTF8));

    [body appendData:[[NSString stringWithFormat:@"url=%@",outputStr] dataUsingEncoding:NSUTF8StringEncoding]];

    //    [body appendData:[[NSString stringWithFormat:@"tinyurl=%@",@"http://dwz.cn/dH14W"] dataUsingEncoding:NSUTF8StringEncoding]];

    

    [request setValue:[NSString stringWithFormat:@"%@",@(body.length)] forHTTPHeaderField:@"Content-Length"];

    [request setHTTPBody:body];

    

    NSURLConnection *con = [[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:NO];

    [con start];


}



- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

{

    NSMutableData *da = [[NSMutableData alloc] init];

    self.data = da;

}


- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{

    [self.data  appendData:data];

}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection

{

    NSLog(@"%@",[[NSString alloc]initWithData:self.data encoding:NSUTF8StringEncoding]);

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

{

    

}

你可能感兴趣的:(ios,url,缩短)