iOS project development--use synchronous request to get web page source code

Source: Internet
Author: User

Network requests generally divided into synchronous requests and asynchronous requests, synchronous requests if the access time is too long, will cause the interface card dead state, the user experience is not very good. However, if the request is faster, you can also consider using synchronous access. Now learn to sync Access first.

(1) Implement the following code in the Viewdidload () method:

    Override Func Viewdidload () {        super.viewdidload ()                var data = Nsurlconnection.sendsynchronousrequest ( Nsurlrequest (Url:nsurl (string: "http://www.baidu.com")!), Returningresponse:nil, Error:nil)                if let d = data{                    println (NSString (data:d, encoding:nsutf8stringencoding)!) Print HTML                    }else{                    println ("Failed to get Network data")}    }

The output will print out the Web page HTML successfully.


(2) Print out the HTTP protocol header

Implement the following code in Viewdidload ():

Override Func Viewdidload () {        super.viewdidload ()                var resp:nsurlresponse?                var data = Nsurlconnection.sendsynchronousrequest (Nsurlrequest (Url:nsurl (string: "http://www.baidu.com")!), Returningresponse: &resp, Error:nil)                if let R = resp{                    println (R)  //Print HTTP protocol header;        }    }

The output results are as follows:


(3) Print error message

In the network operation, the occasional error is unavoidable, we can print out errors, so that we check the wrong, the print error code is as follows:

    Override Func Viewdidload () {        super.viewdidload ()                var resp:nsurlresponse?        var error:nserror?                var data = Nsurlconnection.sendsynchronousrequest (Nsurlrequest (Url:nsurl (string: "http://www.baidu.com")!), Returningresponse: &resp, Error: &error)                if let E = error{                    println (E)  //Print HTTP protocol header;        }    }

When we test the code, we can shut down the network, and then we get an error: When I test, the following errors occur:




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

iOS project development--use synchronous request to get web page source code

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.