douzhouqin6223 2009-10-28 17:47
浏览 31

任何人都可以在此POST NSMutableURLRequest中找到错误到PHP脚本吗?

I try to send data to this php script:

 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'] . ;
    }
...
...

And I'm trying to send a syncronousRequest with this Objc code from iPhone:

//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:@"--%@
",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
 [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"category\"

"] dataUsingEncoding:NSUTF8StringEncoding]];
 [postBody appendData:[[NSString stringWithString:@"iPhone"] dataUsingEncoding:NSUTF8StringEncoding]];
 [postBody appendData:[[NSString stringWithFormat:@"
--%@
",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
 [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"format\"

"] dataUsingEncoding:NSUTF8StringEncoding]];
 [postBody appendData:[[NSString stringWithString:@"jpg"] dataUsingEncoding:NSUTF8StringEncoding]];
 [postBody appendData:[[NSString stringWithFormat:@"
--%@--
",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
 [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"title\"

"] dataUsingEncoding:NSUTF8StringEncoding]];
 [postBody appendData:[[NSString stringWithString:self.theTitle] dataUsingEncoding:NSUTF8StringEncoding]];
 [postBody appendData:[[NSString stringWithFormat:@"
--%@--
",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
 [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"description\"

"] dataUsingEncoding:NSUTF8StringEncoding]];
 [postBody appendData:[[NSString stringWithString:self.theCaption] dataUsingEncoding:NSUTF8StringEncoding]];
 [postBody appendData:[[NSString stringWithFormat:@"
--%@
",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
 [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"photo\"; filename=\"%@.jpg\"
", self.theTitle] dataUsingEncoding:NSUTF8StringEncoding]];
 [postBody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream

"] dataUsingEncoding:NSUTF8StringEncoding]];
 [postBody appendData:[NSData dataWithData:UIImageJPEGRepresentation(resizedImage(self.myPhoto, CGRectMake(0, 0, 600, 800)), 0.5)]];
 [postBody appendData:[[NSString stringWithFormat:@"
--%@--
",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);

But the answer is:

[DEBUG]... response from request: There was an error, all field not existe, please try again!
format =
category =
title =
description =
photo =latitude =
longitude =

What am I doing wrong? because what I see is that no field is received.

  • 写回答

2条回答 默认 最新

  • dongnao3990 2009-10-28 18:12
    关注

    You'd probably get a lot further if your PHP script logged what it actually did receive rather than how it interpreted it.

    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算