PHP 中的 NSMutableURLRequest 长度不完整
我有一个非常烦人的问题,我正在努力解决它。我有一个 iPhone 应用程序,可以将多种形式的数据发送到服务器。服务器端使用 PHP 处理数据并响应。对于小长度的数据,这一切都很好,但其中一个请求非常大,这就不那么顺利了。请求似乎编码良好,具有适当的长度,甚至设置了 Content-Length 标头。我仍然在 PHP 中得到不完整的(字面上被截断的)结果。
下面是一些代码:
NSString *requestString = [NSString stringWithFormat:@"json=%@", [params JSONFragment], nil];
NSLog(@"%@", requestString);
NSLog(@"%u", [requestString length]);
NSData *requestData = [requestString dataUsingEncoding:NSUTF8StringEncoding];
//NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: strlen([requestString UTF8String])]; //[requestString UTF8String]
//NSData *requestData = [NSData dataWithBytes: requestString length: [requestString length]]; //[requestString UTF8String]
//NSData *requestData = [NSData dataWithBytes:[requestString UTF8String] length:[requestString lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"https://radarromance.com/api/index.php"]];
[request setHTTPMethod: @"POST"];
[request setHTTPBody: requestData];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[request setValue:[NSString stringWithFormat:@"%u", [requestString length], nil] forHTTPHeaderField:@"content-length"];
将实际数据视为格式正确的 JSON 字符串,长度为 > 。 4000 个字符。我运行了一个小测试来查看 NSData 对象是否具有正确的长度,结果确实如此。我认为 iPhone 方面一切都很好。
然而,PHP 端仅显示请求的一部分,因此它是不完整的。 post_max_size是16M,应该足够了。
这怎么可能?可能出了什么问题?非常感谢这里的任何帮助!
亲切的问候,
雷德尔
I have a very annoying problem, which I'm trying to solve. I have an iPhone app that sends several forms of data to a server. The server-side processes the data and responds, with PHP. With small lengths of data this goes fine, but one of the requests is pretty large and this doesn't go so well. The requests seems well encoded, with the proper length, even the Content-Length header is set. Still I get an incomplete (literally chopped off) result in PHP.
Here's some code:
NSString *requestString = [NSString stringWithFormat:@"json=%@", [params JSONFragment], nil];
NSLog(@"%@", requestString);
NSLog(@"%u", [requestString length]);
NSData *requestData = [requestString dataUsingEncoding:NSUTF8StringEncoding];
//NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: strlen([requestString UTF8String])]; //[requestString UTF8String]
//NSData *requestData = [NSData dataWithBytes: requestString length: [requestString length]]; //[requestString UTF8String]
//NSData *requestData = [NSData dataWithBytes:[requestString UTF8String] length:[requestString lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"https://radarromance.com/api/index.php"]];
[request setHTTPMethod: @"POST"];
[request setHTTPBody: requestData];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[request setValue:[NSString stringWithFormat:@"%u", [requestString length], nil] forHTTPHeaderField:@"content-length"];
Consider the actual data to be a properly formatted JSON string, with a length of > 4000 characters. I ran a tiny test to see whether the NSData object had the correct length, and it has. On the iPhone side, I assume, all is fine.
The PHP side however shows only a part of the request, so it's incomplete. The post_max_size is 16M, which should be sufficient.
How is this possible? What can possibly be wrong? Any help here is greatly appreciated!
Kind regards,
Reinder
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在使用其他 http 客户端抓取网站时也遇到过这个问题,但也许解决方案是相同的。我的请求不完整,因为我的超时设置得很低,并且下载(或在您的情况下发布)花费了很长时间,因此这会导致我的请求不完整。可能是值得检查的东西!
I've had this problem as well with scraping websites with other http clients, but maybe the solution is the same. I had incomplete requests because my timeout was set to low and downloading (or posting in your case) took to long so it would result in my requests being incomplete. Might be something worth checking!
尝试使用 application/json
try with application/json