iOS发送base64数据的帖子分为两部分

时间:2013-11-13 14:23:08

标签: php ios post base64

这是我的问题:

我正在从iOS设备向我的服务器发送base64编码数据。但有时候,数据似乎分为两个请求。

在设备方面我做:

NSString *base64Data = [self base64forData:myData];
NSMutableURLRequest *myRequest = [NSMutableURLRequest requestWithURL:myRequestURL]; //like www.myaddress.com/myPHP.php

[myRequest setHTTPMethod:@"POST"];

[myRequest setHTTPBody:[[NSString stringWithFormat:@"mydata=%@&type=%d",base64Data] dataUsingEncoding:NSASCIIStringEncoding]];

NSError *error = nil;
NSURLResponse *response = nil;
NSData *result = [NSURLConnection sendSynchronousRequest:myRequest returningResponse:&response error:&error];

这是编码功能:

- (NSString*)base64forData:(NSData*)theData {

    const uint8_t* input = (const uint8_t*)[theData bytes];
    NSInteger length = [theData length];

    static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

    NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
    uint8_t* output = (uint8_t*)data.mutableBytes;

    NSInteger i,i2;
    for (i=0; i < length; i += 3) {
        NSInteger value = 0;
        for (i2=0; i2<3; i2++) {
            value <<= 8;
            if (i+i2 < length) {
                value |= (0xFF & input[i+i2]);
            }
        }

        NSInteger theIndex = (i / 3) * 4;
        output[theIndex + 0] =                    table[(value >> 18) & 0x3F];
        output[theIndex + 1] =                    table[(value >> 12) & 0x3F];
        output[theIndex + 2] = (i + 1) < length ? table[(value >> 6)  & 0x3F] : '=';
        output[theIndex + 3] = (i + 2) < length ? table[(value >> 0)  & 0x3F] : '=';
    }

    return [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
}

在我的服务器端,在myPHP.php中,我使用:$_REQUEST['mydata'];$_REQUEST['type'];file_get_contents('php://input');来检索我的数据。

如果出现问题,第一次,php获取HTTPBody的第一部分。然后,在请求结束时第二次调用它。我可以看到file_get_contents('php://input');完全没有收到我的HTTPBody。例如,我可以在第一次通话中收到mydata的一部分,然后在mydata的{​​{1}}结束时收到。{/ p>

如果我发送:

type

我可以收到它分为

mydata=blablabla&type=totototo

设备端存在问题吗?在base64中转换数据的问题?使用NSASCIIStringEncoding设置HTTPBody?破坏我的服务器检索到的数据的特殊字符?

欢迎任何帮助。

0 个答案:

没有答案