簡體   English   中英

通過SSL HTTPS從iphone發送POST數據

[英]Sending POST data from iphone over SSL HTTPS

海全,

在我的iphone項目中,我需要將用戶名和密碼傳遞給Web服務器,之前我使用GET方法傳遞數據並使用帶有GET格式的URL(例如:localhost:8888 / login?userName = admin&password = password)但是現在我需要將此作為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 echo返回成功或失敗

我終於找到了一種通過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

您正在以NSASCIIStringEncoding發送請求,但正在尋找NSUTF8StringEncoding

我訂了

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

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

然而,正如尼古拉指出的那樣 - 如果我們知道錯誤是什么會更容易:-)

嗯,這完全不是你問題的答案。 但作為替代方案,看看這個代碼。我成功地使用它來向服務器發送用戶名和密碼。

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

希望這可以幫助。

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

然后,使用程序Curl(來自Terminal,它內置)生成一個工作發布請求(您必須在線搜索使用CURL的示例)。 然后,您只需比較CURL的工作請求的格式,以及您的應用程序發送的內容...請注意,模擬器會自動重定向以通過Charles工作,您必須告訴curl用於發送內容的代理地址通過查爾斯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM