iPhone特殊字符ns url帖子

时间:2012-01-17 10:20:27

标签: iphone

我在iPhone工作。我在服务器端使用php 我想发表评论。我正在写这样的代码:

NSString *path=[[NSBundle mainBundle] pathForResource:@"url" ofType:@"plist"];
NSMutableDictionary *tmpDic=[[NSMutableDictionary alloc]initWithContentsOfFile:path];
NSMutableString *tempUrl=[tmpDic valueForKey:@"url1"];  
NSString *urlString=[NSString stringWithFormat:@"%@/CC/postcomment",tempUrl];

NSString *post =[NSString stringWithFormat:@"placeId=%d&user_name=%@&comment=%@&userId=%d",uPlaceId,displayName,[self urlEncodeValue:message],[u.userId intValue]];

NSLog(@"%@",post);     

NSURL *url=[NSURL URLWithString:urlString];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding
                      allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request   setValue:@"application/x-www-form-urlencoded"
 forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

但是当我在评论字符串中写一些特殊字符时,如#$%^那么它会在发送评论时产生问题。

为此我使用了我的评论编码。这是编码功能:

-(NSString *)urlEncodeValue:(NSString *)str {
    NSString *result = (NSString *) 
    CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)str, NULL, CFSTR("?=&+˜!@#$%ˆ&*()_+|\\?'/.>,<;:\"\\'"), kCFStringEncodingUTF8);
    CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)str, NULL, CFSTR("?=&+"), kCFStringEncodingUTF8);

    NSLog(@"%@",result);
    return [result autorelease];
}

工作正常。但是当我想在评论中发送* * 反斜杠时仍然会产生问题 因为以下查询并生成:

insert into place_comments(placeId,comment_datetime,user_name,comment,userId) values('15','2011-02-01','sharmagunjan','\','199') ;

在此查询中,此\在查询中产生问题 请建议我该怎么做。

0 个答案:

没有答案