任何人都可以在这个 POST NSMutableURLRequest 到 PHP 脚本中找到错误吗?
Posted
技术标签:
【中文标题】任何人都可以在这个 POST NSMutableURLRequest 到 PHP 脚本中找到错误吗?【英文标题】:Can anyone find the error in this POST NSMutableURLRequest to a PHP script? 【发布时间】:2009-10-28 17:47:38 【问题描述】:我尝试将数据发送到这个 php 脚本:
mb_internal_encoding("UTF-8");
if(isset($_POST['format']) && isset($_POST['category']) && isset($_POST['title']) && isset($_POST['description']) && isset($_FILES['photo'])
save($_POST['category'], $_POST['title'], $_POST['description'], $_FILES['photo']);
else
echo "There was an error, a field does not exist, please try again!<br />";
echo "format = " . $_POST['format'] . "<br />";
echo "category = " . $_POST['category'] . "<br />";
echo "title = " . $_POST['title'] . "<br />";
echo "description = " . $_POST['description'] . "<br />";
echo "photo =" . $_FILES['photo'] . ;
...
...
我正在尝试从 iPhone 发送带有此 Objc 代码的同步请求:
//creating the url request:
NSURL *cgiUrl = [NSURL URLWithString:@"http://www.hhh.com/uploading.php"];
NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:cgiUrl];
//adding header information:
[postRequest setHTTPMethod:@"POST"];
NSString *stringBoundary = [NSString stringWithString:@"0xKhtmlbOuNdArY"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary];
[postRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];
//setting up the body:
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"category\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"iPhone"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"format\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"jpg"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"title\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:self.theTitle] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"description\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:self.theCaption] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"photo\"; filename=\"%@.jpg\"\r\n", self.theTitle] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[NSData dataWithData:UIImageJPEGRepresentation(resizedImage(self.myPhoto, CGRectMake(0, 0, 600, 800)), 0.5)]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postRequest setHTTPBody:postBody];
NSError *theError;
NSData *returnData = [ NSURLConnection sendSynchronousRequest: postRequest returningResponse: nil error:&theError ];
NSString *returnDataString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"[DEBUG]... response from request: %@", returnDataString);
但答案是:
[DEBUG]... 来自请求的响应: 出现错误,所有字段都没有 存在,请重试!格式化 = 类别 = 标题 = 描述 = 照片 =纬度 = 经度 =
我做错了什么?因为我看到的是没有收到任何字段。
【问题讨论】:
【参考方案1】:如果您的 PHP 脚本记录了它实际接收到的内容,而不是它如何解释它,那么您可能会获得更多信息。
【讨论】:
是的,我正在使用另一个脚本,我一次发送一个值,我发现发送文件时发生了失败的部分......我会继续这个。谢谢【参考方案2】:嗯...解决了一个问题。参数之间的格式必须是这样的:
@"\r\n--%@\r\n"
我正在发送:
@"\r\n--%@--\r\n"
// 多两个分数
现在我还不能发送数据。我在同一台服务器上制作了一个只有此内容的新脚本。
<?php
echo "format = " . $_POST['format'];
echo "[NEXT]... category = " . $_POST['category'];
echo "[NEXT]... title = " . $_POST['title'];
echo "[NEXT]... description = " . $_POST['description'];
echo "[NEXT]... latitude = " . $_POST['latitude'];
echo "[NEXT]... longitude =" . $_POST['longitude'];
?>
并将代码复制到原始脚本(由其他人编写)。
但现在我看到一个脚本接收数据而原来的不能。
会不会是文件编码问题?
【讨论】:
新发现:我使用的url是@"website.com/uploading.com",正确的url是@"website.com/uploading.com" 下一步是找出图片没有上传的原因。跨度>以上是关于任何人都可以在这个 POST NSMutableURLRequest 到 PHP 脚本中找到错误吗?的主要内容,如果未能解决你的问题,请参考以下文章