// 要请求的地址
// 将地址编码
[request setURL:[NSURL URLWithString: urlString]];
[request setCachePolicy:NSURLRequestReloadIgnori
[request setTimeoutInterval: 60];
[request setHTTPShouldHandleCooki
[request setHTTPMethod:@"GET"];
@interface MoreViewController : UIViewController<</span>NSURLConnectionDelegate>
NSString *urlString=@"地址,我就只有保密了!你懂的";
urlString = [urlString stringByAddingPercentEsc
[request setURL:[NSURL URLWithString: urlString]];
[request setCachePolicy:NSURLRequestReloadIgnori
[request setTimeoutInterval: 60];
[request setHTTPShouldHandleCooki
[request setHTTPMethod:@"GET"];
#pragma mark- NSURLConnectionDelegate 协议方法
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)aResponse{
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
}
- (void)connectionDidFinishLoadi
}
- NSData *bodyData = [[bodyString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]dataUsingEncoding:NSUTF8StringEncoding];//把bodyString转换为NSData数据
- NSURL *serverUrl = [[NSURL URLWithString:RequestUrl] URLByAppendingPathComponent:urlStr];//获取到服务器的url地址
- NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:serverUrl
- cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
- timeoutInterval:10];//请求这个地址, timeoutInterval:10 设置为10s超时:请求时间超过10s会被认为连接不上,连接超时
- [request setHTTPMethod:@"POST"];//POST请求
- [request setHTTPBody:bodyData];//body 数据
- [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];//请求头
- NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];//异步发送request,成功后会得到服务器返回的数据
- //返回的数据 根据系统的不同会返回不同编码的数据,比如windows 为GBK,Ubuntu 为UTF8.。。
- //注意转换编码格式