2010-12-21 81 views
0

荫尝试使用HTTP请求值提交到外部数据库问题与Objective C的HTTP请求

我的代码如下

NSString *post =[NSString stringWithFormat:@"user=%@&child=%@&content=%@",userId,childId, crackContent.text]; 

    responseData = [[NSMutableData data] retain]; 
    NSString *hostStr = @"http://myhost.com/addItem.php?"; 
    post = [utilityObj getEncodedUrl:post]; 
    hostStr = [hostStr stringByAppendingString:post]; 
    NSLog(hostStr); 

    NSLog(hostStr); 
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:hostStr]]; 

    [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

我的问题是,如果任何空间包含任何参数,它将不会提交,否则会成功提交。

帮助受到高度赞赏。

回答

1

您可能在发送之前获取参数编码。

NSString* escapedUrlString = 
    [unescapedString stringByAddingPercentEscapesUsingEncoding: 
         NSASCIIStringEncoding]; 

替代

NSString * escapedUrlString = (NSString *)CFURLCreateStringByAddingPercentEscapes(
    NULL, 
    (CFStringRef)unescapedString, 
    NULL, 
    (CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ", 
    kCFStringEncodingUTF8); 
+0

Thaks您的回复,我现在可以解决空间的问题,但是当我加入单引号有一个运行时发生异常时,你知道whts问题。 – user232751 2010-12-21 09:12:01

+0

在答案中添加另一种方法。你可以试试。 – WaiLam 2010-12-22 04:19:35

+0

嗨WaiLam感谢您的回复,我申请了第二个代码,并且我添加了一个单引号,现在没有运行时异常,但数据无法发布。 – user232751 2010-12-23 05:12:11