我有这个URL,我试图通过bit.ly API缩短它。这是我的代码
NSString *url = @"http://www.example.com&username=abc&password=123&mode=offline";
NSString *requestStr = [NSString stringWithFormat:@"http://api.bit.ly/v3/shorten?login=%@&apiKey=%@&longUrl=%@&format=txt",login, api_key, url];
requestStr = [requestStr stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:requestStr]];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *response = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
我收到的回复网址只对应http://www.example.com
令人惊讶的是,& username = abc& password = 123& mode =修剪网址的离线部分。 只有当我通过xcode执行此操作时才会发生这种情况。在网站上,它正常工作。请帮助。
答案 0 :(得分:1)
NSString *url = @"https://www.googleapis.com/urlshortener/v1/url?key=UR_KEY";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"UR_LONG_URL",@"longUrl", nil];
[request setHTTPBody:[[dict JSONRepresentation] dataUsingEncoding:NSUTF8StringEncoding]];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *response = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];