1

每当我尝试将某些内容发布到我的 PHP 服务器时,我都会收到以下消息。似乎代码正在连接到服务器,但没有返回任何数据,并且发布数据没有通过。它通过我制作的 Java 应用程序运行,所以我可以保证他们的 PHP 没有问题。如果您可以帮助我,或者需要更多代码来帮助我,请提出要求。谢谢。

这是为 NSURLConnection 准备变量的代码:

NSString *phash = [NSString stringWithFormat:@"%d",phashnum];
        [phash stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
name = _nameField.text;
        [name stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        email = _emailField.text;
        [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

这是我的 NSURLConnection 的代码:

NSString *urlPath = [NSString stringWithFormat:@"http://54.221.224.251"];
    NSURL *url = [NSURL URLWithString:urlPath];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    NSString *stringdata = [NSString stringWithFormat:@"name=%@&email=%@&phash=%@",name,email,phash];
    NSOperationQueue *queue= [[NSOperationQueue alloc]init];
    NSString *postData = [[NSString alloc] initWithString:stringdata];
    [request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:[postData dataUsingEncoding:NSUTF8StringEncoding]];
    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if ([data length] > 0 && connectionError==nil){
            NSLog(@"Connection Success. Data Returned");
            NSLog(@"Data = %@",data);
            NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
            int code = [httpResponse statusCode];
            NSString *coder = [NSString stringWithFormat:@"%d",code];
            NSLog(@"%@",coder);
        }
        else if([data length] == 0 && connectionError == nil){
            NSLog(@"Connection Success. No Data returned.");
            NSLog(@"Connection Success. Data Returned");
            NSLog(@"Data = %@",data);
            NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
            int code = [httpResponse statusCode];
            NSString *coder = [NSString stringWithFormat:@"%d",code];
            NSLog(@"%@",coder);
        }
        else if(connectionError != nil && connectionError.code == NSURLErrorTimedOut){
            NSLog(@"Connection Failed. Timed Out");
            NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
            int code = [httpResponse statusCode];
            NSString *coder = [NSString stringWithFormat:@"%d",code];
            NSLog(@"%@",coder);

        }
        else if(connectionError != nil)
        {
            NSLog(@"%@",connectionError);
            NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
            int code = [httpResponse statusCode];
            NSString *coder = [NSString stringWithFormat:@"%d",code];
            NSLog(@"%@",coder);

        }
    }];

提前致谢。

4

2 回答 2

1

"name=%@&email=%@&phash=%@" 不是正确的 url 编码字符串。每个键值对必须用“&”字符分隔,每个键与其值之间用“=”字符分隔。键和值都通过用“+”字符替换空格进行转义,然后用stringByAddingPercentEscapesUsingEncoding.

请参阅application/x-www-form-urlencoded

您可以在此博客文章中找到如何做到这一点的食谱。

于 2013-08-30T22:02:59.857 回答
1

正如@elk 所说,您应该将空格替换为+. 但是您应该对保留字符进行百分比编码(如RFC2396中所定义)。

不幸的是,该标准stringByAddingPercentEscapesUsingEncoding并未对所有保留字符进行百分比转义。例如,如果名称是“Bill & Melinda Gates”或“Bill + Melinda Gates”,stringByAddingPercentEscapesUsingEncoding则不会百分比转义 the&或 the +(因此 the+将被解释为空格,而 the&将被解释为分隔下一个POST范围)。

相反,使用CFURLCreateStringByAddingPercentEscapes,在参数中提供必要的保留字符,legalURLCharactersToBeEscaped然后用 . 替换空格+。例如,您可以定义一个NSString类别:

@implementation NSString (PercentEscape)

- (NSString *)stringForPostParameterValue:(NSStringEncoding)encoding
{
    NSString *string = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                                 (CFStringRef)self,
                                                                                 (CFStringRef)@" ",
                                                                                 (CFStringRef)@";/?:@&=+$,",
                                                                                 CFStringConvertNSStringEncodingToEncoding(encoding)));
    return [string stringByReplacingOccurrencesOfString:@" " withString:@"+"];
}

@end

请注意,我主要关心的是&and +, 字符,但是RFC2396(它取代了 RFC1738)将这些附加字符列为保留字符,因此将所有这些保留字符包含在legalURLCharactersToBeEscaped.

综合起来,我可能有将请求发布为的代码:

NSDictionary *params = @{@"name" : _nameField.text ?: @"",
                         @"email": _emailField.text ?: @"",
                         @"phash": [NSString stringWithFormat:@"%d",phashnum]};

NSURL *url = [NSURL URLWithString:kBaseURLString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

[request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[self httpBodyForParamsDictionary:params]];

[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    if (error)
        NSLog(@"sendAsynchronousRequest error = %@", error);

    if (data) {
        // do whatever you want with the data
    }
}];

使用实用方法:

- (NSData *)httpBodyForParamsDictionary:(NSDictionary *)paramDictionary
{
    NSMutableArray *paramArray = [NSMutableArray array];
    [paramDictionary enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) {
        NSString *param = [NSString stringWithFormat:@"%@=%@", key, [obj stringForPostParameterValue:NSUTF8StringEncoding]];
        [paramArray addObject:param];
    }];

    NSString *string = [paramArray componentsJoinedByString:@"&"];

    return [string dataUsingEncoding:NSUTF8StringEncoding];
}
于 2013-08-31T03:37:29.383 回答