我正在尝试将电子邮件订阅到我的 EC2 服务器上运行的Sendy 安装- 可以在此处找到 Sendy 的 API 文档。
在我的清单中,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 中用于订阅列表。我可以确认这可行 - 请参阅我的网站。
但是,我需要通过 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 认为该电子邮件无效- 无论如何,该电子邮件未注册。