NSURL请求/NSURL连接不工作



我得到了下面的代码来调用PHP脚本,出于某种原因,它说连接成功,但实际上它没有调用我的脚本,因为我还没有收到电子邮件:-(

未调用任何NSURLConnectionDelegate方法。

在iPhone 5.x设备和模拟器上测试。

有什么建议/想法吗?非常感谢!

NSString* url = [NSString stringWithFormat:@"http://www.lenniedevilliers.net/mail.php?subject=%@&to=%@&body=%@", subjectLine, toAddress, emailBody ];
NSLog(@"web service URL: %@", url);
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    connection = [[NSURLConnection alloc] initWithRequest: request delegate:self];
NSLog(@"connection: %@", [connection debugDescription]);
if (connection) {
    NSLog(@"Connection succeeded");
} else {
    NSLog(@"Connection failed");
}
NSString* url = [NSString stringWithFormat:@"http://www.lenniedevilliers.net/mail.php?subject=%@&to=%@&body=%@", subjectLine, toAddress, emailBody ];
NSLog(@"web service URL: %@", url);
//add the following or something like
[url setString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
//modify the following    
connection = [[NSURLConnection alloc] initWithRequest: request delegate:self startImmediately:YES]; 
NSLog(@"connection: %@", [connection debugDescription]);
if (connection) {
    NSLog(@"Connection succeeded");
} else {
    NSLog(@"Connection failed");
}

我敢打赌,你的电子邮件正文中有一些内容通过url 发送是不安全的

NSString* url = [NSString stringWithFormat:@"http://www.lenniedevilliers.net/mail.php?subject=%@&to=%@&body=%@", subjectLine, toAddress, emailBody ];

假设您的电子邮件正文包含&,这会破坏您的代码。您要么需要转义消息正文,要么通过请求正文发送消息,而不是通过URL传递消息。我可能错了,但我敢打赌就是这样。你的代码看起来很好,我敢打赌是数据;)

您是否使用NSURLConnection的特定委托函数?

- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@"Did Receive Response %@", response);
    responseData = [[NSMutableData alloc]init];
}
- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{
    //NSLog(@"Did Receive Data %@", data);
    [responseData appendData:data];
}
- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
{
    NSLog(@"Did Fail");
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"Did Finish");
    // Do something with responseData
}

最新更新