OC, oc Language
# NSURLRequest
NSURLRequest encapsulates the data required for a network request, and mainly encapsulates the following information:
- Request Path (URL)
- Request Method (GET or POST)
- Request Header
- Request body
- Timeout Parameter
NSURLRequest and its subclass NSMutableURLRequest
- All the request information of the NSURLRequest is spliced after the Request Path (URL ).
- The Request Path of NSMutableURLRequest is separated from other request information. Other request information is set through the corresponding Key.
- NSURLRequest is usually used for GET requests.
- NSMutableURLRequest is usually used for POST requests.
Procedure for NSURLRequest to encapsulate a network request
// 1. Create the Request Path NSString * strURL = [NSString stringWithFormat: @ "(URL here)/login? Username = % @ & pwd = % @ ", @" username ", @" password "]; NSURL * url = [NSURL URLWithString:]; // 2. encapsulate the request NSURLRequest * request = [NSURLRequest requestWithURL: url] According to the request Path;
NSMutableURLRequest to encapsulate a network request
// 1. create the Request Path NSURL * url = [NSURL URLWithString: @ "(URL here)/login"]; // 2. CREATE request NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL: url]; // 3. set the request Method request. HTTPMethod = @ "POST"; // 4. set request parameters. HTTPBody = [@ "username =" username "& pwd =" password "dataUsingEncoding: NSUTF8StringEncoding]; // 5. set timeout request. timeoutInterval = 5;
# NSURLConnection
# Chinese processing in URLs
The text in the URL is to be processed, typically using UTF-8 Encoding
// Perform the following transcoding: [urlStr stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]
PS. In Xcode7, NSURLConnection has been abolished and cannot be used any more. Now NSURLSession is used instead.