我使用下面的代码来连接HTTP服务器。在这里,我将如何修改我的代码,以便通过UITextField的输入提供用户名和密码,并且下次将使用将保存在NSUserDefaults中的相同数据进行连接。
NSString * urlAsString = @“http://abc.com/services/user/validate?email=abc@gmail.com&password=123456”;
//urlAsString = [urlAsString stringByAppendingString:@"?email=abc@gmail.com"];
//urlAsString = [urlAsString stringByAppendingString:@"&pass=123456"];
NSURL *url = [NSURL URLWithString:urlAsString];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setTimeoutInterval:30.0f];
[urlRequest setHTTPMethod:@"POST"];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response,NSData *data, NSError *error) {
if ([data length] >0 && error == nil){
NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"HTML = %@", html);
receivedData = [[NSMutableData data] retain];
}
else if ([data length] == 0 && error == nil){
NSLog(@"Nothing was downloaded.");
}
else if (error != nil){
NSLog(@"Error happened = %@", error);
} }];
答案 0 :(得分:1)
NSString *urlAsString = [NSString stringWithFormat:
@"http://abc.com/services/user/validateemail=%@&password=%@",
myUsernameTextField.text, // abc@gmail.com
myPasswordTextField.text ]; // 1234565