【问题标题】:Image upload to SSL folder, status code 200, headers {connection = closed}图片上传到 SSL 文件夹,状态码 200,标题 {connection = closed}
【发布时间】: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 宣布的新标准,但上传任何图像仍然没有运气。

任何帮助将不胜感激,谢谢。

【问题讨论】:

    标签: php ios ssl


    【解决方案1】:

    请注意,200 是“OK”的 HTTP 状态代码。请求处理成功时返回。

    由于您收到 JSON 响应

    {"code":4000,"message":"fail"}
    

    可以推断出条件语句

    if (isset($_POST['comentario']) && isset($_POST['id']))
    

    返回 false。

    这并不奇怪,因为您没有在 POST 请求中发送字段 commentarioid

    您必须包含这些字段或对服务器端 php 代码进行修改。

    由于缺少字段,您可能还希望在发送错误响应时返回另一个 HTTP 状态代码。我建议“错误请求”为 400。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-25
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-11
      相关资源
      最近更新 更多