OGeek|极客世界-中国程序员成长平台

标题: ios - 如何在 iOS 应用程序中连接到 EC2 实例 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 08:48
标题: ios - 如何在 iOS 应用程序中连接到 EC2 实例

我有一个 AWS EC2 实例和一个我创建的应用程序。该应用程序适用于偏头痛患者(跟踪信息,告诉他们触发因素是什么)。现在我希望能够将用户输入从我的应用程序发送到服务器,以便我可以看到趋势。我无法连接到服务器并找出如何将文件写入服务器。

这个方法我写过:

- (void) sendDataToServerNSString *)url NSString *)key : (NSString *) content{

    // define your form fields here:
    //NSString *content = @"field1=42&field2=Hello";


    NSString *address = [NSString stringWithFormat"ssh -i %@ %@", key, url];
    NSLog(@"%@", address);
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:address]];
    [request setHTTPMethod"OST"];
    NSData *contentData = [content dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:contentData];
    NSString *postLength = [NSString stringWithFormat"%d",[contentData length]];
    [request setValue:postLength forHTTPHeaderField"Content-Length"];

    // generates an autoreleased NSURLConnection
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if (conn){
        NSLog(@"connection");
    }
    //[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                               //[self doSomethingWithData:data];
                               if (error){
                                   NSLog(@"ERROR");
                               }
                           }];

}

我将 key 对中的私钥放入应用程序。我将如何使用它来连接?我不应该使用我的私钥吗?我应该采取不同的做法吗?



Best Answer-推荐答案


Should I be doing this differently?

绝对,100%,是的。您不想让人们通过 SSH 连接到您的服务器,尤其是将您的私钥嵌入到应用程序二进制文件中。有人很容易得到它,然后对您的服务器造成严重破坏。

不要这样做。

相反,我会得到一个像 Apache 这样的网络服务器在您的实例上运行(这很简单),并编写一个将文件保存到服务器硬盘驱动器的应用程序(在 PHP、Rails(带有Passenger)、Python 等中)。您还需要获得 Elastic IP address就像 ashack 提到的那样,它保持不变。

在您的 iOS 应用程序中,您需要向您的服务器发送一个 POST 请求。见 Sending an HTTP POST request on iOS ,基本上就是你现在正在做的事情。

不要发布您的私有(private) SSH key 。它是私有(private)的,这是有充分理由的。

关于ios - 如何在 iOS 应用程序中连接到 EC2 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25082921/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4