iOS:“502 Bad Gateway。服务器返回无效或不完整的响应。”

时间:2013-12-14 08:54:33

标签: php ios upload gateway

当我尝试将图片上传到php服务器时,我收到此错误。你知道问题是什么吗?它是由应用程序还是Web服务器引起的?这是我上传图片和一些字符串的代码。有趣的是,如果我尝试上传视频 - 没有问题。当我尝试上传图片时 - 糟糕的网关。

    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [path objectAtIndex:0];

    //NSString *urlString = @"http://pik.bg/add_android_pics.php?id=127718";
    NSString *urlString = [NSString stringWithFormat:@"http://pik.bg/news/edit_news_android/%@", uniqueID];
    NSMutableURLRequest *requestUpload = [[NSMutableURLRequest alloc] init];
    //NSMutableURLRequest *requestUpload = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:30];
    [requestUpload setURL:[NSURL URLWithString:urlString]];
    [requestUpload setHTTPMethod:@"POST"];


    NSString *boundary = @"AaB03x";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
    [requestUpload addValue:contentType forHTTPHeaderField: @"Content-Type"];

        //now lets create the body of the post

    NSMutableData *body = [NSMutableData data];

    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Disposition: form-data; name=\"title\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:heading.text] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Disposition: form-data; name=\"description\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:text.text] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    if (camera == YES || gallery == YES)
    {
        NSString *fileNameOne = [NSString stringWithFormat:@"uploadedImageOne.jpg"];
        NSString *imgPathWithFileNameOne = [documentDirectory stringByAppendingPathComponent:fileNameOne];
        [UIImageJPEGRepresentation(savedImage, 90) writeToFile:imgPathWithFileNameOne atomically:YES];

        NSData *imageData = [NSData dataWithContentsOfFile:imgPathWithFileNameOne];

        [body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file1\"; filename=\"image.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSASCIIStringEncoding]];
        [body appendData:[NSData dataWithData:imageData]];
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

        if (secondImage != nil)
        {
            NSString *fileNameTwo = [NSString stringWithFormat:@"uploadedImageTwo.jpg"];
            NSString *imgPathWithFileNameTwo = [documentDirectory stringByAppendingPathComponent:fileNameTwo];
            [UIImageJPEGRepresentation(secondImage, 90) writeToFile:imgPathWithFileNameTwo atomically:YES];

            NSData *imageDataTwo = [NSData dataWithContentsOfFile:imgPathWithFileNameTwo];

            [body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
            [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file2\"; filename=\"image2.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSASCIIStringEncoding]];
            [body appendData:[NSData dataWithData:imageDataTwo]];
            [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        }

        if (thirdImage != nil)
        {
            NSString *fileNameThree = [NSString stringWithFormat:@"uploadedImageThree.jpg"];
            NSString *imgPathWithFileNameThree = [documentDirectory stringByAppendingPathComponent:fileNameThree];
            [UIImageJPEGRepresentation(thirdImage, 90) writeToFile:imgPathWithFileNameThree atomically:YES];

            NSData *imageDataThree = [NSData dataWithContentsOfFile:imgPathWithFileNameThree];

            [body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
            [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file3\"; filename=\"image3.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSASCIIStringEncoding]];
            [body appendData:[NSData dataWithData:imageDataThree]];
            [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        }
    }

    if (video == YES)
    {
        NSData *videoData = [NSData dataWithContentsOfFile:videoString];

        [body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"video_mp4\"; filename=\"%@.mp4\"\r\nContent-Type: video/mp4\r\n\r\n", uniqueName] dataUsingEncoding:NSASCIIStringEncoding]];
        [body appendData:[NSData dataWithData:videoData]];
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    }

    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Disposition: form-data; name=\"submit\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"1"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];


// setting the body of the post to the reqeust
    [requestUpload setHTTPBody:body];

// now lets make the connection to the web
    NSData *returnData = [NSURLConnection sendSynchronousRequest:requestUpload returningResponse:nil error:nil];

    NSString *returnStringer = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

    NSLog (@"Return String: %@", returnStringer);

我认为问题不在我的应用中,你怎么看?

0 个答案:

没有答案