Ios-get Request verbose Operation-get Set the request header

Source: Internet
Author: User

Rare once notes quite detailed native get network request operation, like obsessive-compulsive disorder recorded and shared ... Also ready for replication

-(void) getresult{_MB = [Mbprogresshud showhudaddedto: Self. Navigationcontroller. ViewAnimated:YES];//interface path    NSString*path = @"Http://a.apix.cn/apixlife/phone/phone";//path-+ parameter    NSString*pathwithphonenum = [NSStringstringwithformat:@"%@?phone=%@", PATH,_PHONENUMFD. Text];//Chinese code    NSString*urlpath = [Pathwithphonenum stringbyaddingpercentencodingwithallowedcharacters:[nscharacterset Urlqueryallowedcharacterset]];//url    Nsurl*phoneurl = [NsurlUrlwithstring:urlpath];//Request ObjectNsmutableurlrequest *request = [Nsmutableurlrequest Requestwithurl:phoneurl];//Request Method[Request sethttpmethod:@"GET"];//Request Header[Request setvalue:@"92b5787ecd17417b718a2aaedc7e6ce8"forhttpheaderfield:@"Apix-key"];//Network configurationNsurlsessionconfiguration *configuration = [Nsurlsessionconfiguration defaultsessionconfiguration];//Network sessionNsurlsession *session = [Nsurlsession sessionwithconfiguration:configuration];//TasksNsurlsessiondatatask *sessiontask = [Session datataskwithrequest:request completionhandler:^ (NSData * _Nullable data, Nsurlresponse * _nullable Response,Nserror* _nullable error) {//Return to the main thread update UI, Undo Matte        Dispatch_async(Dispatch_get_main_queue (), ^{[_mb Hide:YES]; });if(Error) {NSLog(@"Request failed ...%@", error);//Prompt user request failed!Uialertcontroller *av = [Uialertcontroller alertcontrollerwithtitle:@"Hint"message:@"Sorry, server error, please try again later ..."Preferredstyle:uialertcontrollerstyleactionsheet]; [AV addaction:[uialertaction actionwithtitle:@"OK"Style:uialertactionstyledefault handler:^ (uialertaction * _nonnull action) {//Click OK to perform the operation, you can place nil                NSLog(@"You clicked OK ...");            }]]; [ SelfPresentviewcontroller:av Animated:YESCompletion:Nil]; }Else{//json Analysis of Apple native efficiency highest            nsdictionary*result = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutableleaves Error:Nil];if([[Result objectforkey:@"Message"] isequaltostring:@"Success"]) {//Get data--Main thread update UI                Dispatch_async(Dispatch_get_main_queue (), ^{nsdictionary*data = [Result objectforkey:@"Data"];NSString*city = [Data objectforkey:@"City"];NSString*province = [Data objectforkey:@"province"];NSString*belong = [NSStringstringwithformat:@"%@ · %@", province,city];                [_resultlb Settext:belong];            }); }Else{NSLog(@"No information found ..."); }NSLog(@"Request succeeded ...%@", result); }    }];//Start Task[Sessiontask resume];}

Ios-get Request verbose Operation-get Set the request header

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.