手机号查询POST请求

{

UITextField *fi;

UITextView *vi;

NSMutableData *data1;

}

#define  SJH @"http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo"



{

[super viewDidLoad];

UILabel *lab=[[UILabel alloc]initWithFrame:CGRectMake(20, 40, 100, 44)];

lab.text=@"手机号";

[self.view addSubview:lab];

fi=[[UITextField alloc]initWithFrame:CGRectMake(60, 40, 150, 44)];

[self.view addSubview:fi];

vi=[[UITextView alloc]initWithFrame:CGRectMake(30, 150, 300, 200)];

vi.backgroundColor=[UIColor redColor];

[self.view addSubview:vi];

//设置按钮

UIButton *bu=[[UIButton alloc]initWithFrame:CGRectMake(50, 90, 100, 44)];

bu.backgroundColor=[UIColor redColor];

[bu setTitle:@"查询" forState:UIControlStateNormal];

[bu addTarget:self action:@selector(fan) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:bu];

}

-(void)fan

{

//设置接受

NSURL *url=[NSURL URLWithString:SJH];

//

NSMutableURLRequest *mu=[[NSMutableURLRequest alloc]initWithURL:url];

//设置请求方式

[mu setHTTPMethod:@"POST"];

//设置传递参数

NSString *stee=[NSString stringWithFormat:@"mobileCode=%@&userID=",fi.text];

//格式转换

NSData *da=[stee dataUsingEncoding:NSUTF8StringEncoding];

//设置需要传递参数

[mu setHTTPBody:da];

//

//

//    //

//    NSData *da2=[NSURLConnection sendSynchronousRequest:mu returningResponse:nil error:nil];

//

//    vi.text=[[[NSString alloc]initWithData:da2 encoding:NSUTF8StringEncoding]substringWithRange:NSMakeRange(78, 26)];

//设置代理

[NSURLConnection connectionWithRequest:mu delegate:self];

}

//请求服务器成功后自动回调 执行一次

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;

{

//初始化

data1=[[NSMutableData alloc]init];

}

//成功读取服务器端数据后自动回调 执行多次

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;

{

//拼接数据

[data1 appendData:data];

}

//请求结束后自动回调

- (void)connectionDidFinishLoading:(NSURLConnection *)connection;

{

//展示转换格式

vi.text=[[NSString alloc]initWithData:data1 encoding:NSUTF8StringEncoding];

}

你可能感兴趣的:(手机号查询POST请求)