iOS Network nsurlconnection POST

Source: Internet
Author: User

1#import"HMViewController.h"2#import"mbprogresshud+mj.h"3 4 @interface Hmviewcontroller ()5@property (Weak, nonatomic) Iboutlet Uitextfield *Usernamefield;6@property (Weak, nonatomic) Iboutlet Uitextfield *Pwdfield;7-(ibaction) login;8 @end9 Ten @implementation Hmviewcontroller One  A  - /** - * Login Logic the  */ --(ibaction) login - { -     //1. Form validation (input validation) +NSString *username =Self.usernameField.text; -     if(Username.length = =0) {//no user name entered +[Mbprogresshud ShowError:@"Please enter user name"]; A         return; at     } -      -NSString *pwd =Self.pwdField.text; -     if(Pwd.length = =0) {//no password entered -[Mbprogresshud ShowError:@"Please enter your password"]; -         return; in     } -      to     //Bullet Box +[Mbprogresshud ShowMessage:@"Trying desperately to log in ..."]; -      the     //2. Send the request to the server (with the account number and password) *     //POST request: Request body $     Panax Notoginseng     //2.1. Set the request path -Nsurl *url = [Nsurl urlwithstring:@"Http://192.168.1.200:8080/MJServer/login"]; the      +     //2.2. Create a Request object ANsmutableurlrequest *request = [Nsmutableurlrequest Requestwithurl:url];//The default is a GET request theRequest.timeoutinterval =5;//Set Request Timeout +Request. HttpMethod =@"POST";//Set as POST request -      $     //tell the server client type by the request header $[Request SetValue:@"iOS"Forhttpheaderfield:@"user-agent"]; -      -     //set the request body theNSString *param = [NSString stringWithFormat:@"username=%@&pwd=%@", username, pwd]; -Request. Httpbody =[param datausingencoding:nsutf8stringencoding];Wuyi      the     //2.3. Sending the request -Nsoperationqueue *queue =[Nsoperationqueue Mainqueue]; Wu[Nsurlconnection sendasynchronousrequest:request queue:queue completionhandler:^ (NSURLResponse *response, NSData * Data, Nserror *connectionerror) {//called when the request is over (the server's data is received, the request fails) -         //Hide Hud (Refresh UI, be sure to put it on the main thread, not on child threads) About [Mbprogresshud Hidehud]; $          -         /** - Parse Data: - {"Error": "User name does not exist"} A {"Error": "Password is incorrect"} + {"Success": "Login Succeeded"} the          */ -         if(data) {//Request succeeded $Nsdictionary *dict =[nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutableleaves Error:nil]; theNSString *error = dict[@"Error"]; the             if(Error) {//Logon Failure the [Mbprogresshud Showerror:error]; the}Else{//Login Successful -NSString *success = dict[@"Success"]; in [Mbprogresshud showsuccess:success]; the             } the}Else{//request failed About[Mbprogresshud ShowError:@"the network is busy, please try again later"]; the         } the     }]; the } +  -@end

iOS Network nsurlconnection POST

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.