首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >图片上传到SSL文件夹,状态码200,标题{connection = closed}

图片上传到SSL文件夹,状态码200,标题{connection = closed}
EN

Stack Overflow用户
提问于 2015-06-17 19:52:02
回答 1查看 193关注 0票数 0

我试图连接到我的服务器上我的SSL文件夹中的php文件,上传在我的应用程序中拍摄的图像,所有证书等都已就位,目录的权限设置为777,但我收到一个错误代码200,没有上传的图像。

在我的ViewController.m中与此相关的代码部分是

代码语言:javascript
复制
- (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文件是

代码语言:javascript
复制
<?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);
}



?>

错误响应为

代码语言:javascript
复制
   <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宣布的新标准,但仍然不能上传任何图像。

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

EN

回答 1

Stack Overflow用户

发布于 2015-06-17 22:44:12

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

因为您正在接收JSON响应

代码语言:javascript
复制
{"code":4000,"message":"fail"}

可以推断出条件语句

代码语言:javascript
复制
if (isset($_POST['comentario']) && isset($_POST['id']))

返回false。

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

您将不得不包括这些字段或修改服务器端的php代码。

当由于缺少字段而发送错误响应时,您可能还希望返回另一个HTTP状态代码。我建议对"Bad request“使用400。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30890790

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档