2013-08-30 39 views
1

每當我嘗試發佈一些東西到我的PHP服務器,我收到以下消息。好像代碼連接到服務器,但沒有數據被返回,並且發佈數據沒有經過。它通過我製作的Java應用程序工作,所以我可以保證他們對我的PHP沒有任何問題。如果你能幫助我,或者需要更多的代碼來幫助我,那就問問它。謝謝。NSURLConnection連接到服務器,但不發佈數據

這裏是準備我的變量爲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); 

     } 
    }]; 

在此先感謝。

+0

使用AFNetworking,它使你的生活變得更加簡單! https://github.com/AFNetworking/AFNetworking –

回答

1

正如@elk所說,您應該用+替換空格。但是您應該百分比編碼保留字符(如RFC2396中所定義)。

不幸的是,標準stringByAddingPercentEscapesUsingEncoding沒有百分比轉義所有的保留字符。例如,如果名稱是「Bill & Melinda Gates」或「Bill + Melinda Gates」,則stringByAddingPercentEscapesUsingEncoding將不會百分比轉義&+(因此+將被解釋爲空格,並且&將會是解釋爲界定下一個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 

注意,我主要關心的是&+,字符,但是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]; 
} 
1

「name =%@ & email =%@ & phash =%@」不是一個正確的url編碼字符串。每個鍵值對都必須用'&'字符分隔,每個鍵的值由'='字符分隔。鍵和值都通過用'+'字符替換空格而被轉義,然後由stringByAddingPercentEscapesUsingEncoding進行編碼。

參見application/x-www-form-urlencoded

你可以在this blog post找到一個配方如何做到這一點。

+0

我該如何糾正它? –

+0

當我對它進行百分比編碼時,我不是在做什麼? –

+0

我更新了上面的代碼,它工作,服務器接受它。 –

相關問題