IOS network NSURLConnection, iosnsurlconnection
NSURLRequest
- Used to save the request address, request header, and request body
- By default, NSURLRequest automatically sets the Request Header for us.
- Request is a GET request by default.
- NSURLConnection and RunLoop
By default, NSURLConnection is added to the RunLoop by the current thread. If NSURLConnection is called in the Child thread, it may be faulty because the child thread does not have the RunLoop by default.
How to make the proxy method run in the Child thread?
- [Conn setDelegateQueue: [[NSOperationQueue alloc] init];
Note:
NSURLConnection is an asynchronous request.
// 1. send request NSURLRequest * request = [NSURLRequest requestWithURL: [NSURL URLWithString: @ "http: // XXXXX/resources/images/logs"]; nsunloop * runloop = [nsunloop currentRunloop]; NSURLConnection * conn = [NSURLConnection connectionWithRequest: request delegate: self]; [conn setDelegateQueue: [[NSOperationQueue alloc] init]; // 2. start runLoop // by default, the sub-thread does not have a RunLoop, so you need to start [runloop run]; // 3. if a request is sent manually, the system automatically creates a RunLoop // NSURLConnection * conn = [[NSURLConnection alloc] initWithRequest: request delegate: self startImmediately: YES]; // [conn start];
Synchronous request
- If you call the NSURLConnection synchronization method, the current thread will be blocked.
// 1. Create a url nsurl * url = [NSURL URLWithString: @ "http: // XXXXXXX/login? Username = XXX & pwd = XXX & type = JSON "]; // 2. create an NSURLRequest object NSURLRequest * request = [NSURLRequest requestWithURL: URL] According to the url; // 3. use the NSURLConnection object to send a request/* First parameter: The second parameter of the object to be requested: The response header information returned by the Service. Third parameter: returned error message: response body returned by the server */NSHTTPURLResponse * response = nil; // actual NSData * data = [NSURLConnection sendSynchronousRequest: request returningResponse: & response error: nil]; NSLog (@ "% @", [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]); NSLog (@ "response = % @", response. allHeaderFields );
- Asynchronous request
// 1. Create a url nsurl * url = [NSURL URLWithString: @ "http: // XXXXX/login? Username = XXX & pwd = XXX & type = JSON "]; // 2. create an NSURLRequest object NSURLRequest * request = [NSURLRequest requestWithURL: URL] According to the url; // 3. use the NSURLConnection object to send a request/* First parameter: The second parameter of the requested object: the queue of the callback block, which determines the thread in which the block executes the third parameter: callback block * // Note: If the NSURLConnection synchronization method is called, the current thread [NSURLConnection sendAsynchronousRequest: request queue: [NSOperationQueue mainQueue] completionHandler: ^ (NSURLResponse * response, NSData * data, NSError * connectionError) {NSLog (@ "% @", [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]);}];
- POST method
// 1. create a url nsurl * url = [NSURL URLWithString: @ "http: // XXXXX/login"]; // 2. create an NSURLRequest object NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL: URL] According to the url; // 2.1 set the request METHOD // Note: POST must be capitalized. HTTPMethod = @ "POST"; // 2.2 set the Request body // Note: If the parameter is passed to the POST request: Do you need to write it? Request. HTTPBody = [@ "username = XXX & pwd = XXX & type = JSON" dataUsingEncoding: NSUTF8StringEncoding]; // 3. use the NSURLConnection object to send the request [NSURLConnection sendAsynchronousRequest: request queue: [NSOperationQueue mainQueue] completionHandler: ^ (NSURLResponse * response, NSData * data, NSError * connectionError) {NSLog (@ "% @", [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]);}];
- Request Server Response
// 1. create a url nsurl * url = [NSURL URLWithString: @ "http: // XXXXX/resources/images/minion_02.png"]; // 2. create NSURLRequest * request = [NSURLRequest requestWithURL: URL] According to the url; // 3. the system will automatically send the [[NSURLConnection alloc] initWithRequest: request delegate: self]; * // startImmediately: if YES is passed, the system will automatically send the request. If NO is passed, the system will not automatically send the request NSURLConnection * conn = [[NSURLConnection alloc] initWithRequest: request delegate: self startImmediately: NO]; [conn start]; */[NSURLConnection connectionWithRequest: request delegate: self];
Proxy Method
# Pragma mark-NSURLConnectionDataDelegate/* calls response: response Header */-(void) connection :( NSURLConnection *) connection didReceiveResponse :( NSURLResponse *) response {NSLog (@ "% s", _ func _);}/* Call when receiving data returned by the server (This method may be called once or multiple times) data: data returned by the server (not the total number currently passed to us) */-(void) connection :( NSURLConnection *) connection didReceiveData :( NSData *) data {NSLog (@ "% s", _ func _);}/* call at end of receiving */-(void) connectionDidFinishLoading :( NSURLConnection *) connection {NSLog (@ "% s", _ func _);}/* call in case of a request error (request timeout) */-(void) connection :( NSURLConnection *) connection didFailWithError :( NSError *) error {NSLog (@ "% s", _ func __);}
- Chinese questions