iOS之数据请求NSURLConnection
2018-07-20 来源:open-open
iOS之数据请求NSURLConnection NSString *lcsUrl = @"http://192.168.1.1:8080/lcsUrl"; //如果网址中有汉字,需要先转码 NSString *word = [NSString stringWithUTF8String:"汉字"];//注意此处到字符串要求传一个c的字符串,没有@ NSString *finaUrl = [NSString stringWithFormat:@"http://192.168.1.1:8080/lcsUrl= %@",word]; NSURL *url = [NSURL URLWithString:lcsUrl]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url]; request.HTTPMethod = @"GET"; request.timeoutInterval = 60; NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self]; [connection start]; 请求出错 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"请求出错"); } 请求中接收到数据 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [_data appendData:data]; } 请求完毕 - (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSString *str = [[NSString alloc] initWithData:_data encoding:NSUTF8StringEncoding]; //在这里处理你收到的数据 }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
最新资讯
热门推荐