在我的iphone项目中,我需要将成员Id和pwd传递给Web服务器。我已尝试使用Synchronous和异步请求的代码,但我总是得到错误:连接失败:错误的URL(null)。任何人都可以帮我找到代码有什么问题吗?
我的代码:
NSString *post =[NSString stringWithFormat:@"memberId=%@&pwd=%@",txtUName.text,txtPwd.text];
NSLog(@"%@",post);
NSURL *url = [NSURL
URLWithString:@"http://testweb.talkfusion.com/videocenter/xlogin.asp%@"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding
allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
//set up the request to the website
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"%@",data);
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
答案 0 :(得分:0)
你网址末尾的%@是错误的。 POST数据不是网址的一部分。