无标题文章

//Get 服务器连接地址
#define TEST_URL @"http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode=%@&userID="

//Post 服务器连接地址
#define POST_URL @"http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo"
@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
 
}



//get请求
- (IBAction)Get:(id)sender
{
    //1.连接字符串
    NSString *str1=[NSString stringWithFormat: TEST_URL,self.myNumberTF.text];
    
    //2.拿到URL 字符串
    NSURL *url= [NSURL URLWithString:str1];
    
    //3.将连接封装成 请求对象
    NSURLRequest *quest=[[NSURLRequest alloc]initWithURL:url];
    //4.Get 请求代理
    NSData *data=[NSURLConnection sendSynchronousRequest:quest returningResponse:nil error:nil];
    //5.返回数据到Textviewd<展示信息>
    self.JieGuoTextfield.text=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
}

//Post请求
- (IBAction)Post:(id)sender
{
    //1.创建URL 地址
    NSURL *url =[NSURL URLWithString:POST_URL];
    
    NSMutableURLRequest*requst=[[NSMutableURLRequest alloc]initWithURL:url];
    
    [requst setHTTPMethod:@"POST"];
    
    NSString *str=[NSString stringWithFormat:@"mobileCode=%@&userID=",self.myNumberTF.text];
    
    NSData *data=[str dataUsingEncoding:NSUTF8StringEncoding];
    
    [requst setHTTPBody:data];
    
    NSData *myDatas =[NSURLConnection sendSynchronousRequest:requst returningResponse:nil error:nil];
    
    self.JieGuoTextfield.text =[[NSString alloc]initWithData:myDatas encoding:NSUTF8StringEncoding];
}

你可能感兴趣的:(无标题文章)