【问题标题】:Sending POST data from iphone over SSL HTTPS通过 SSL HTTPS 从 iphone 发送 POST 数据
【发布时间】:2010-12-06 23:30:21
【问题描述】:

大家好,

在我的 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 返回成功或失败

【问题讨论】:

  • 你的代码怎么不工作?
  • 我的意思是,它到底是怎么失败的?有什么错误?它崩溃了吗?您在哪里看到问题,在服务器上还是在您的客户端代码中?
  • 嘿,我发现了问题,因为我使用的是 HTTPS
  • 嗨@shinto Joseph,我正在创建类似于你的应用程序,我正在使用 get 方法将用户名和密码传递给网络服务器。但我遇到了如何传递它的问题。你能帮我吗跨度>
  • 我知道这是一个老问题,但我想知道你的 php 代码是什么。

标签: iphone-sdk-3.0 iphone


【解决方案1】:

您正在以 NSASCIIStringEncoding 的形式发送请求,但正在寻找 NSUTF8StringEncoding

我会设置

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

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

然而,正如 Nikolai 所指出的 - 如果我们知道错误是什么,这会更容易:-)

【讨论】:

    【解决方案2】:

    嗯,这完全不是您问题的答案。但作为替代方案,请查看此代码。我成功使用此代码将用户名和密码发送到服务器。

    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
    

    希望这会有所帮助。

    【讨论】:

    • @norskben : 虽然代码在 HTTP 上工作,但尚未通过 HTTPS 测试过
    • 是的,问题要求使用 HTTPS。
    【解决方案3】:

    要调试 HTTP 问题,最好的办法是获取 Charles HTTP 代理应用程序 - 它会通过模拟器记录与服务器之间的所有 HTTP 通信(您甚至可以将其设置为手机的代理)如果需要)。

    然后,使用程序 Curl(来自 Terminal,它是内置的)生成一个工作发布请求(您必须在线搜索使用 CURL 的示例)。然后,您只需将来自 CURL 的工作请求的格式与您的应用程序发送的内容进行比较...请注意,模拟器会自动重定向到通过 Charles 工作,您必须告诉 curl 用于发送内容的代理地址通过查尔斯。

    【讨论】:

      【解决方案4】:

      我终于找到了一种通过安全连接从 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

      【讨论】:

      • 注意:setAllowsAnyHTTPSCertificate 是一个私有 API。请参阅stackoverflow.com/questions/2001565/… 了解更多信息。
      • 注意,这段代码会让你的应用被拒绝,setAllowsAnyHTTPSCertificate:forHost: 是一个私有API
      • 如果用户 id 或密码包含 & 符号会中断
      • 如何检查是否保持安全连接?
      猜你喜欢
      • 1970-01-01
      • 2010-09-19
      • 2013-05-24
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-27
      相关资源
      最近更新 更多