2009-10-15 41 views
26

海所有發送POST數據,從iphone通過SSL HTTPS

在我的iPhone項目,我需要的用戶名和密碼傳遞到Web服務器,以前我通過使用GET方法的數據和使用GET格式的URL (如:本地主機:8888 /登錄用戶名=管理員密碼& =密碼?),但現在我需要發送這是一個POST數據,

任何一個可以幫助我找到什麼錯在下面這段代碼?

代碼我試過..


NSString *post =[NSString stringWithFormat:@"userName=%@&password=%@",userName.text,password.text]; 

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
[request setURL:[NSURL URLWithString:@"https://localhost:443/SSLLogin/login.php"]]; 
[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); 

這個頁面將返回成功或失敗的使用PHP呼應

+2

如何不工作的代碼? – 2009-10-15 10:00:59

+2

是的,它不起作用 – 2009-10-15 10:11:56

+0

我的意思是,它究竟是如何失敗?什麼是錯誤?它崩潰了嗎?你在哪裏看到問題,在服務器或客戶端代碼中? – 2009-10-15 10:37:32

回答

9

您正在發送的請求爲NSASCIIStringEncoding但尋找NSUTF8StringEncoding

我d set

 
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; 

 
[request setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 

然而,由於尼古拉指出 - 如果我們知道是什麼錯誤:-)

8

嗯,這會更容易些,這到底是不是一個回答你的問題。但作爲替代方案,請查看此代碼。我成功地將此用戶名和密碼發送到服務器。

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:loginURL]]; 

//set HTTP Method 
[request setHTTPMethod:@"POST"]; 

//Implement request_body for send request here username and password set into the body. 
NSString *request_body = [NSString stringWithFormat:@"Username=%@&Password=%@",[Username stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], [Password stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 
//set request body into HTTPBody. 
[request setHTTPBody:[request_body dataUsingEncoding:NSUTF8StringEncoding]]; 

//set request url to the NSURLConnection 
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 


if(theConnection) //get the response and retain it 

然後,您可以實現以下委託檢查響應

-(void)connectionDidFinishLoading:(NSURLConnection *)connection 

希望這有助於。

+0

不清楚這是否通過HTTPS。 – oberbaum 2012-06-18 18:18:13

+0

@norskben:當代碼工作通過HTTP,還沒有通過HTTPS – 2012-06-19 07:57:26

+0

是啊,問題問的HTTPS測試這一點。 – oberbaum 2012-06-19 19:29:11

0

要調試HTTP問題,最好的辦法是獲取Charles HTTP代理應用程序 - 它將通過模擬器記錄與服務器之間的所有HTTP通信(並且您甚至可以將其設置爲手機的代理服務器)if你需要)。

然後,使用Curl程序(來自終端,它是內置的)來生成工作崗位請求(您必須在線搜索使用CURL的示例)。然後,您只需比較一下CURL的工作請求是如何格式化的,以及您的應用程序發送的內容是什麼......請注意,模擬器會自動重新定向以通過Charles工作,您必須告訴curl使用代理地址來發送事件通過查爾斯。

28

我終於得到了一個方式,爲來自iPhone的安全連接發送數據:

NSString *post =[[NSString alloc] initWithFormat:@"userName=%@&password=%@",userName.text,password.text]; 
NSURL *url=[NSURL URLWithString:@"https://localhost:443/SSLLogin/Login.php"]; 

NSLog(post); 
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 

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]; 

/* when we user https, we need to allow any HTTPS cerificates, so add the one line code,to tell teh NSURLRequest to accept any https certificate, i'm not sure about the security aspects 
*/ 

[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]]; 

NSError *error; 
NSURLResponse *response; 
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 

NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding]; 
NSLog(@"%@",data); 

避免一條警告消息,請加


@interface NSURLRequest (DummyInterface) 
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host; 
+ (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host; 
@end 

禮貌:

  1. 我所有的堆棧在流動的朋友和支持者
  2. http://www.cocoadev.com/index.pl?HTTPFileUpload
  3. http://blog.timac.org/?tag=nsurlrequest
+0

這個工作完美的我 – 2010-12-15 06:14:48

+1

注:setAllowsAnyHTTPSCertificate是一個私有API。見http://stackoverflow.com/questions/2001565/alternative-method-for-nsurlrequests-private-setallowsanyhttpscertificateforho獲取更多信息。 – 2010-12-24 21:03:07

+4

注意,這個代碼將讓你的應用程序被拒絕,setAllowsAnyHTTPSCertificate:forHost:是私有API – oberbaum 2012-06-18 18:09:43