如何将此HTML表单转换为iOS

时间:2013-04-12 21:31:43

标签: ios nsurlconnection nsurlrequest

我正在尝试订阅我的EC2服务器上运行的 Sendy安装的电子邮件 - 可以找到Sendy的API文档here

在我的列表中,Sendy建议

<form action="http://www.erwaysoftware.com/sendy/subscribe" method="POST" accept-charset="utf-8">
    <label for="name">Name</label><br/>
    <input type="text" name="name" id="name"/>
    <br/>
    <label for="email">Email</label><br/>
    <input type="text" name="email" id="email"/>
    <br/>
    <label for="App">App</label><br/>
    <input type="text" name="App" id="App"/>
    <br/>
    <input type="hidden" name="list" value="my_sendy_list_key"/>
    <input type="submit" name="submit" id="submit"/>
</form>

在HTML中用于订阅列表。 我可以确认这是有效的 - 请参阅my website.

但是,我需要通过API 从我的应用中订阅这些人。这是我尝试过的代码:

- (IBAction)submitButtonPressed:(id)sender
{
    self.data = [[NSMutableData alloc] initWithLength:0];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.erwaysoftware.com/sendy/subscribe/"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"john@me.com" forHTTPHeaderField:@"email"];
    [request setValue:@"John Bob" forHTTPHeaderField:@"name"];
    [request setValue:@"api_key" forHTTPHeaderField:@"list"];
    [request setValue:@"TRUE" forHTTPHeaderField:@"boolean"];

    NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];
    [conn start];

}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [self.data setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d {
    [self.data appendData:d];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", @"")
                                 message:[error localizedDescription]
                                delegate:nil
                       cancelButtonTitle:NSLocalizedString(@"OK", @"")
                       otherButtonTitles:nil] show];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSString *responseText = [[NSString alloc] initWithData:self.data encoding:NSUTF8StringEncoding];

    // Do anything you want with it

    NSLog(@"%@", responseText);
}

NSLog 显示:

2013-04-12 15:23:03.917 Sendy API Test[13488:c07] <!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link rel="Shortcut Icon" type="image/ico" href="http://www.erwaysoftware.com/sendy/img/favicon.png">
        <title>Subscribed</title>
    </head>
    <style type="text/css">
        body{
            background: #ffffff;
            font-family: Helvetica, Arial;
        }
        #wrapper 
        {
            background: #f2f2f2;

            width: 300px;
            height: 70px;

            margin: -140px 0 0 -150px;
            position: absolute;
            top: 50%;
            left: 50%;
            -webkit-border-radius: 5px;
            -moz-border-radius: 5px;
            border-radius: 5px;
        }
        p{
            text-align: center;
        }
        h2{
            font-weight: normal;
            text-align: center;
        }
        a{
            color: #000;
        }
        a:hover{
            text-decoration: none;
        }
    </style>
    <body>
        <div id="wrapper">
            <h2>Email address is invalid.</h2>
        </div>
    </body>
</html>

据此,我认为Sendy认为该电子邮件无效 - 无论如何,该电子邮件未注册。

1 个答案:

答案 0 :(得分:2)

问题是您正在设置HTTP请求标头。 API期望信息出现在HTTP 正文中。不幸的是,NSMutableURLRequest类没有提供创建正文的简单方法。 This问题提供了手动构建请求正文的示例代码。

也就是说,自己构建请求体是很棘手的,因为格式化规则可能很神秘。更好的方法是使用NSURLRequest之一的开源扩展,它将为您处理。我在我当前的项目中使用AFNetworking,虽然设置起来有点复杂。