【问题标题】:Why this POST don't work?为什么这个帖子不起作用?
【发布时间】:2011-02-02 06:22:34
【问题描述】:


这是我的 sn-p:

NSString *post = @"from=A&name=B&email=ciccio@aaasd.com&messaggio=MESSAGE&codHidden=-1";
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];    

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"http://www.mysite.com/page.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData]; 

NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSLog(@"Succeeded! Received %d bytes of data",[urlData length]);
NSString *outputdata = [[NSString alloc] initWithData:urlData encoding:NSASCIIStringEncoding];
NSLog(@"%@", outputdata);

它调用一个向我发送电子邮件的 php 页面......

这段代码有错误吗?
好像不行……

谢谢

【问题讨论】:

  • 我们还需要 PHP 脚本的内容。那里可能有问题。
  • 好的。 objc 代码对你有用吗?
  • NSLog() 函数记录了什么?
  • 什么都没有。页面没有返回值...

标签: iphone objective-c nsurlconnection http-post nsmutableurlrequest


【解决方案1】:

是同步/异步问题吗?如果是这样,请使用委托模式并异步访问数据。您能否发布您看到的任何错误(或服务器的响应字符串)?

编辑:由于您对返回的错误没有做任何有用的事情,请更改您的类以执行请求异步。对此的一个很好的答案以前发布过here。一旦你实施了didFailWithError,你就会有一个更好的画面。

【讨论】:

  • 没有错误,没有响应(因为页面返回一个空字符串),也没有邮件!代码正确吗?如果是,我必须订购更改/修复 php 页面...
  • 嗯,首先,您不会将 NSError 响应发送到您可以在方法返回后进行调查的对象。所以,把上面的改成NSError *returnedError; NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&returnedError];
【解决方案2】:

尝试将 NSASCIIStringEncoding 改为 NSUTF8StringEncoding

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-13
    • 1970-01-01
    • 2013-07-21
    • 1970-01-01
    • 2015-02-23
    • 2020-05-29
    • 1970-01-01
    相关资源
    最近更新 更多