Sync request, asynchronous request, get request, POST request for iOS

Source: Internet
Author: User

1, the synchronization request can request data from the Internet, once the synchronization request is sent, the program will stop the user interaction until the server returns the data to complete before the next operation,

2, the asynchronous request does not block the main thread, but will establish a new thread to operate, after the user makes an asynchronous request, still can work on the UI, the program can continue to run

3, get request, the parameters are written directly on the access path. Simple operation, but easy to be seen by the outside, security is not high, address up to 255 bytes;

4, POST request, put the parameters inside the body. Post request operation is relatively complex, need to separate parameters and addresses, but high security, parameters placed inside the body, not easily captured.

?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 6667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611 7118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 1、     同步GET请求    //第一步,创建URL    NSURL *url = [NSURL URLWithString:@"http://api.hudong.com/iphonexml.do?type=focus-c"];        //第二步,通过URL创建网络请求    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];    //NSURLRequest初始化方法第一个参数:请求访问路径,第二个参数:缓存协议,第三个参数:网络请求超时时间(秒)      其中缓存协议是个枚举类型包含:      NSURLRequestUseProtocolCachePolicy(基础策略)      NSURLRequestReloadIgnoringLocalCacheData(忽略本地缓存)       NSURLRequestReturnCacheDataElseLoad(首先使用缓存,如果没有本地缓存,才从原地址下载)      NSURLRequestReturnCacheDataDontLoad(使用本地缓存,从不下载,如果本地没有缓存,则请求失败,此策略多用于离线操作)      NSURLRequestReloadIgnoringLocalAndRemoteCacheData(无视任何缓存策略,无论是本地的还是远程的,总是从原地址重新下载)      NSURLRequestReloadRevalidatingCacheData(如果本地缓存是有效的则不下载,其他任何情况都从原地址重新下载)    //第三步,连接服务器    NSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];        NSString *str = [[NSString alloc]initWithData:received encoding:NSUTF8StringEncoding];        NSLog(@"%@",str);2、同步POST请求     //第一步,创建URL    NSURL *url = [NSURL URLWithString:@"http://api.hudong.com/iphonexml.do"];    //第二步,创建请求    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];    [request setHTTPMethod:@"POST"];//设置请求方式为POST,默认为GET    NSString *str = @"type=focus-c";//设置参数    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];    [request setHTTPBody:data];    //第三步,连接服务器         NSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];        NSString *str1 = [[NSString alloc]initWithData:received encoding:NSUTF8StringEncoding];        NSLog(@"%@",str1);3、异步GET请求    //第一步,创建url    NSURL *url = [NSURL URLWithString:@"http://api.hudong.com/iphonexml.do?type=focus-c"];    //第二步,创建请求    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];     //第三步,连接服务器    NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];4、异步POST请求    //第一步,创建url    NSURL *url = [NSURL URLWithString:@"http://api.hudong.com/iphonexml.do"];    //第二步,创建请求    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];    [request setHTTPMethod:@"POST"];    NSString *str = @"type=focus-c";    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];     [request setHTTPBody:data];    //第三步,连接服务器    NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];5、异步请求的代理方法//接收到服务器回应的时候调用此方法- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{    NSHTTPURLResponse *res = (NSHTTPURLResponse *)response;    NSLog(@"%@",[res allHeaderFields]);    self.receiveData = [NSMutableData data];                                 }//接收到服务器传输数据的时候调用,此方法根据数据大小执行若干次-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{    [self.receiveData appendData:data];}//数据传完之后调用此方法-(void)connectionDidFinishLoading:(NSURLConnection *)connection {    NSString *receiveStr = [[NSString alloc]initWithData:self.receiveData encoding:NSUTF8StringEncoding];    NSLog(@"%@",receiveStr);}//网络请求过程中,出现任何错误(断网,连接超时等)会进入此方法-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{    NSLog(@"%@",[error localizedDescription]);

Sync request, asynchronous request, get request, POST request for iOS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.