我正在尝试将一些 XML 格式的数据发送到我可以注册用户的网页。
我尝试了两种方法,并用两种我感觉的方式来获取数据。
方式一
NSString *urlString = @"http://****.com/iphone/csi/register.php?choice=register";
NSURL *url = [NSURL URLWithString: urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: url];
[request setHTTPMethod: @"POST"];
[request setValue: @"text/xml" forHTTPHeaderField: @"Content-Type"];
NSMutableData *highScoreData = [NSMutableData data];
[highScoreData appendData: [[NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"] dataUsingEncoding: NSUTF8StringEncoding]];
[highScoreData appendData: [[NSString stringWithFormat: @"<user_name>%@</user_name", @"test"] dataUsingEncoding: NSUTF8StringEncoding]];
[highScoreData appendData: [[NSString stringWithFormat: @"<password>%@</password>", @"test"] dataUsingEncoding: NSUTF8StringEncoding]];
[highScoreData appendData: [[NSString stringWithFormat: @"</xml>"] dataUsingEncoding: NSUTF8StringEncoding]];
[request setHTTPBody: highScoreData];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest: request
delegate: self];
方式二
NSString * xmlString = @"<test><message>Hello</message></test>";
NSURL * serviceUrl = [NSURL URLWithString:@"http://demos4clients.com/iphone/csi/register.php?choice=register"];
NSMutableURLRequest * serviceRequest = [NSMutableURLRequest requestWithURL:serviceUrl];
[serviceRequest setValue:@"text/xml" forHTTPHeaderField:@"Content-type"];
[serviceRequest setHTTPMethod:@"POST"];
[serviceRequest setHTTPBody:[xmlString dataUsingEncoding:NSASCIIStringEncoding]];
NSURLResponse * serviceResponse;
NSError * serviceError;
serviceResponse = [NSURLConnection sendSynchronousRequest:serviceRequest returningResponse:&serviceResponse error:&serviceError];
我怎样才能找到这个错误的原因?