【发布时间】:2015-06-17 11:52:02
【问题描述】:
我正在尝试连接到服务器上 SSL 文件夹中的 php 文件,以上传在我的应用程序中拍摄的图像,所有证书等都已到位,并且目录的权限设置为 777,但我收到错误代码200 并且没有上传图片。
我的 ViewController.m 中与此相关的代码部分是
- (IBAction)subirImagen:(id)sender {
NSData *imageData = UIImagePNGRepresentation(self.imagen.image);
NSString *urlString = @"https://secure101.prositehosting.co.uk/php-upload-file-server-master2/subir.php";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body initMultipart];
[body addPart:@"myFile" withFileName:@"imagen.png" withNSData:imageData];
[body addPart:@"myName" withValue:@"Ricardo"];
[body writeLastBoundary];
[request setHTTPBody:body];
NSURLResponse* response = nil;
NSError* err = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSHTTPURLResponse *HTTPURLResponse = (NSHTTPURLResponse *)response;
NSInteger statusCode = [HTTPURLResponse statusCode];
NSString* mensaje = [NSString stringWithFormat:@"Request: %ld",(long)statusCode];
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Server"
message:mensaje
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[myAlertView show];
NSLog(@"Response Meta: %@", response);
NSLog(@"String sent from server %@",[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
//As was advised
NSHTTPURLResponse* newResp = (NSHTTPURLResponse*)response;
//crash here
NSLog(@"%ld", (long)newResp.statusCode);
}
php文件是
<?php
header('Content-Type: application/json; charset=utf-8');
//header('Content-Type: text/html; charset=utf-8');
if (isset($_POST['comentario']) && isset($_POST['id'])) {
$comentario = $_POST['comentario'];
$id = $_POST['id'];
if (isset($_FILES['myFile'])) {
move_uploaded_file($_FILES['myFile']['tmp_name'], "./" . $_FILES['myFile']['name']);
}
$respuesta = array("code"=>2000,"message"=>"successful", "comentario"=>$comentario, "id"=>$id);
// $class_response = json_decode(json_encode($respuesta));
// echo $class_response->comentario;
//echo $comentario;
echo json_encode($respuesta);
}
else{
$respuesta = array("code"=>4000,"message"=>"fail");
echo json_encode($respuesta);
}
?>
错误响应是
<NSHTTPURLResponse: 0x1545b4360> { URL: https://secure101.prositehosting.co.uk/php-upload-file-server-master2/subir.php } { status code: 200, headers {
Connection = close;
"Content-Type" = "application/json; charset=utf-8";
Date = "Wed, 17 Jun 2015 10:55:46 GMT";
Server = Apache;
"Transfer-Encoding" = Identity;
} }
2015-06-17 11:55:55.556 CameraApp[1647:643453] String sent from server {"code":4000,"message":"fail"}
我已经在 info.plist 文件中的 Xcode 中实现了 TLS 密钥,以符合 iOS 9 宣布的新标准,但上传任何图像仍然没有运气。
任何帮助将不胜感激,谢谢。
【问题讨论】: