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];
//在这里处理你收到的数据
}
原文地址:http://blog.csdn.net/lu_ca/article/details/46379137