在iphone中使用url编码使用%2b之前添加+ [英] adding + before using %2b using url encoding in iphone

查看:230
本文介绍了在iphone中使用url编码使用%2b之前添加+的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,在按钮单击时我通过我的服务器api方法调用JSON post方法并将数据保存到服务器数据库。这里我将我的手机号码和紧急号码保存到服务器数据库。我的手机号码是在我的手机号码字符串变量中,我的手机号码正在保存,格式为+ 90-9491491411。我输入+然后编码然后 - 然后编号但是当我发送到服务器数据库时我正在删除 - 并将no发送到数据库但问题是在我的服务器数据库+移动没有进入我正在输入。可能是什么问题。我正在使用POST方法发送请求。这是我的代码

I have an application where on a button click i am passing my server api method which calls the JSON post method and saves data to server database.Here i am saving my mobile number and emergency no to server database.My mobile number is in string format.In my mobile number string variable, my mobile number is getting saved which is in this format '+90-9491491411'.I am entering + and then code and then- and then number but when i send to the server database i am removing the - and sending the no to database but problem is in my server database + of mobile is not getting entered which i am entering .What may be the problem .I am using POST method to send the request .This is my code

-(void)sendRequest
{

    NSString *newstring = txtMobile.text;
    mobileValue = [newstring stringByReplacingOccurrencesOfString:@"-" withString:@""];
    NSLog(@"%@",mobileValue);



    NSString *newString1 = txtemergencyprovider.text;
    emergencyNumber = [newString1 stringByReplacingOccurrencesOfString:@"-" withString:@""];


        NSLog(@"%@",emergencyNumber);
    if ([txtEmail.text  isEqualToString:@""])
    {
        post = [NSString stringWithFormat:@"CommandType=new&ApplicationType=%d&FullName=%@&Mobile=%@&EmergencymobileNumber=%@&Latitude=%f&Longitude=%f&City=%@&MobileModel=Apple",applicationtype,txtFullname.text,mobileValue,emergencyNumber,latitude,longitude,txtCity.text];
        NSLog(@"%@",post);
    }
    else {
        post = [NSString stringWithFormat:@"CommandType=new&ApplicationType=%d&FullName=%@&Mobile=%@&EmergencymobileNumber=%@&Latitude=%f&Longitude=%f&City=%@&EmailAddress=%@&MobileModel=Apple",applicationtype,txtFullname.text,mobileValue,emergencyNumber,latitude,longitude,txtCity.text,txtEmail.text];
        NSLog(@"%@",post);
    }


    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 
    NSLog(@"%@",postLength);
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
    [request setURL:[NSURL URLWithString:@"http://myapi?RequestType=NEW"]]; 
    [request setHTTPMethod:@"POST"]; 
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
    [request setHTTPBody:postData];

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    if (theConnection) {
        webData = [[NSMutableData data] retain];
        NSLog(@"%@",webData);
    }
    else 
    {

    }

}

//在我的手机号码和紧急号码变量中,我的号码格式为+91986444711,但是当在服务器数据库中输入值时,将被删除。可能是概率

//In my mobile number and emrgency number variable my no is in this format '+91986444711' but when the value is entered in server database + gets removed off .What may be the prob.

推荐答案

不幸的是, NSString -stringByAddingPercentEscapesUsingEncoding:不会将加号( + )符号转换为%2B ,因为加号是一个有效的URL字符,用于分隔查询参数。这通常意味着Web服务器将其转换为空格字符。

Unfortunately, NSString's -stringByAddingPercentEscapesUsingEncoding: will not convert the plus (+) sign into %2B because the plus sign is a valid URL character that is used to separate query parameters. What this usually means is that it gets converted to a space character by the web server.

替换加号的最简单方法是使用 NSString stringByReplacingOccurrencesOfString: withString:%2B 替换 + 。例如:

The easiest way to replace the plus sign would be using NSString's stringByReplacingOccurrencesOfString:withString: to replace the + with %2B. For example:

mobileValue = [mobileValue stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];

这篇关于在iphone中使用url编码使用%2b之前添加+的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆