NSString *urlString = @"http://127.0.0.1:8081/register";
NSString *post = [NSString stringWithFormat: @"productId=80&userName=%@&password=%@&email=%@",username.text,mustr,email.text];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSLog(@"%@",[request description]);
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLResponse *reponse;
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&reponse error:&error];
if (error != nil) {
NSLog(@"Something wrong: %@",[error description]);
[Utils alertWithTitle: @"" message: @"注册失败,请检查您的网络"];
}else {
if (responseData) {
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSData *data = [responseString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
//设置该类本身为代理类,即该类在声明时要实现NSXMLParserDelegate委托协议
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
[parser setDelegate:self];
[parser parse];
NSString *result = [self.xmlDict objectForKey:@"result"];
NSString *code = [self.xmlDict objectForKey:@"code"];
if([result isEqualToString:@"1"]){
[Utils alertWithTitle: @"注册成功" message: @"并且自动登录"];
UserUtil *uu = userUtil;
uu.username = username.text;
uu.password = password.text;
uu.isLogin = YES;
[self dismissView];
}
if([code isEqualToString:@"2003"]){
[Utils alertWithTitle: @"注册失败" message: @"用户名已经被其他用户所使用"];
}else if([code isEqualToString:@"2004"]){
[Utils alertWithTitle: @"注册失败" message: @"未知错误"];
}
self.xmlDict = nil;
[responseString release];
responseString = nil;
[parser release];
parser = nil;
}
}
IOS请求WEB应用的方法
最新推荐文章于 2018-10-19 14:20:10 发布