我已经编写了同伴代码:
NSString *urlString = [NSString stringWithFormat:ADDRESS,action];
postStr = @"user_name=Thomas Tan&phone=01234567891&password=123456";
NSData *myRequestData = [NSData dataWithBytes:[postStr UTF8String] length:[postStr length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody: myRequestData];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *responseString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSLog(@"%@",responseString);
它运行良好,但现在我想使用 asihttprequest 框架,那么如何更改上面的代码,我已经编写了代码,但它无法得到正确的结果,只是得到服务器错误信息。所以有什么问题?
NSString *urlString = [NSString stringWithFormat:ADDRESS,action];
NSURL *url = [NSURL URLWithString:urlString];
ASIFormDataRequest *requeset = [ASIFormDataRequest requestWithURL:url];
[requeset setRequestMethod:@"POST"];
[requeset setPostValue:@"Thomas Tan" forKey:@"user_name"];
[requeset setPostValue:@"01234567891" forKey:@"phone"];
[requeset setPostValue:@"123456" forKey:@"password"];
[requeset startSynchronous];
NSError *error = [requeset error];
if (!error) {
NSString *re = [requeset responseString];
NSLog(@"%@",re);
}
NSLog(@"%@",error);
先感谢您。
更新:
NSString *urlString = [NSString stringWithFormat:ADDRESS,action];
NSURL *url = [NSURL URLWithString:urlString];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setRequestMethod:@"POST"];
[request appendPostData:[@"user_name=Thomas Tan&phone=01234567891&password=123456" dataUsingEncoding:NSUTF8StringEncoding]];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSString *re = [request responseString];
NSLog(@"%@",re);
}
NSLog(@"%@",error);
我使用上面的代码,它也不能得到相同的结果,并且错误不是零。