URL 接続の次の 2 つの方法の違いを知る必要がありますか? これら 2 つの方法の重要性は何ですか? どのような状況でどの方法が好まれますか?
方法 1: file.h #import
#define kPostURL @"http://localhost/php/register.php"
#define kemail @"email"
#define kpassword @"password"
@interface signup : UIViewController
{
...
...
NSURLConnection *postConnection;
}
@property ...
...
@end
file.m
NSMutableDictionary *input=[[NSMutableDictionary alloc]init];
...
...
...
NSMutableString *postString = [NSMutableString stringWithString:kPostURL];
[postString appendString: [NSString stringWithFormat:@"?%@=%@", kemail, [input objectForKey:@"email"] ]];
[postString appendString: [NSString stringWithFormat:@"&%@=%@", kpassword, [input objectForKey:@"password"] ]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:postString]];
[request setHTTPMethod:@"POST"];
postConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
NSLog(@"postconnection: %@", postConnection);
NSData *dataURL = [NSData dataWithContentsOfURL: [ NSURL URLWithString: postString ]];
NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];
NSLog(@"serverOutput = %@", serverOutput);
方法 2:
NSString *post =[NSString stringWithFormat:@"email=%@&password=%@",[input objectForKey:@"email"], [input objectForKey:@"password"]];
NSString *hostStr = @"http://localhost/frissbee_peeyush/php/login.php?";
hostStr = [hostStr stringByAppendingString:post];
NSLog(@"HostStr %@",hostStr);
NSData *dataURL = [NSData dataWithContentsOfURL: [ NSURL URLWithString: hostStr ]];
NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];
NSLog(@"serverOutput %@", serverOutput);
ヘッダー情報のみを使用して URL に接続する必要がある場合はどうすればよいですか?
問題: ログインとサインアップの場合、これらの方法はまったく問題ありませんが、@、/、_ などの特殊文字を含む文字列を挿入しようとすると、操作を実行できません。
ガイドしてください。