为什么这个帖子不起作用?

发布于 2024-10-15 17:31:55 字数 1196 浏览 3 评论 0原文


这是我的代码片段:

NSString *post = @"from=A&name=B&[email protected]&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 页面...

这段代码有任何错误吗?
似乎不起作用...

谢谢

this is my snippet:

NSString *post = @"from=A&name=B&[email protected]&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);

It call a php page that send me an email...

Is there any errors in this code?
Does not seem to work...

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

九局 2024-10-22 17:31:55

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

编辑:由于您没有对返回的错误执行任何有用的操作,因此更改您的类以异步执行请求。之前在此处发布了对此问题的一个很好的答案。一旦你实现了didFailWithError,你就会有一个更好的画面。

Is it a Synchronous/Asynchronous problem? If so, use the delegate-pattern and access the data asynchronously. Could you post any errors you see (or the server's response string)?

EDIT: Since you're not doing anything useful with the error returned, change your class to do the request asynchronously. A great answer to this was previously posted here. Once you implement didFailWithError you'll have a better picture.

囍孤女 2024-10-22 17:31:55

尝试将 NSASCIIStringEncoding 更改为 NSUTF8StringEncoding

Try to change NSASCIIStringEncoding to NSUTF8StringEncoding

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文