2011-09-30 51 views
0

在我的iPhone項目中,我需要將成員ID和密碼傳遞給Web服務器。我試過我的代碼與同步以及異步請求,但我總是得到錯誤:連接失敗:錯誤的URL(空)。任何人都可以幫我找到代碼有問題嗎?連接失敗:將POST數據從Xcode發送到傳統ASP頁時發生錯誤Url(空)錯誤。

我的代碼:

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

在您的網址末尾的%@是錯誤的。 POST數據不是網址的一部分。

+0

我從網址中刪除了%@,它工作正常。謝謝。 – user973637