美文网首页
iOS原生请求 post 参数包含 & 失败的解决方案

iOS原生请求 post 参数包含 & 失败的解决方案

作者: 小明2021 | 来源:发表于2023-03-22 19:02 被阅读0次

最近自己用NSMutableURLRequest 写post请求的时候,遇到的问题:

post的某一个参数里面包含 & 由于默认的url编码不会把 & 转义,所以导致请求的时候,把 & 当成了拼接参数的符号了,导致把参数拆分并请求失败

失败的原因是:

请求的时候url是用 & 符号拼接的,所以url的编码是不会把 & 转义的,但是参数里面如果包含 & 就需要想办法转义了。 平时之所以没遇到这个问题,是因为AFNetworking已经帮我们做了这部分工作。

解决方案:

确保 参数字典转成json的时候,保证每一个参数里面都不包含 & ,可以手动把 & 转义成 %26 ,也可以用网上的方案统一编码。 请求的时候,把 json 正常转成 data 就OK了。

AFNetworking 处理类似问题的解决方案是:

思路是不用系统的字典转json,而是自己用for循环把每一个字典的字段都用 & 收到拼接下,并且把每个字段值的特殊字符都转义了。

/**
 Returns a percent-escaped string following RFC 3986 for a query string key or value.
 RFC 3986 states that the following characters are "reserved" characters.
    - General Delimiters: ":", "#", "[", "]", "@", "?", "/"
    - Sub-Delimiters: "!", "$", "&", "'", "(", ")", "*", "+", ",", ";", "="

 In RFC 3986 - Section 3.4, it states that the "?" and "/" characters should not be escaped to allow
 query strings to include a URL. Therefore, all "reserved" characters with the exception of "?" and "/"
 should be percent-escaped in the query string.
    - parameter string: The string to be percent-escaped.
    - returns: The percent-escaped string.
 */
NSString * AFPercentEscapedStringFromString(NSString *string) {
    static NSString * const kAFCharactersGeneralDelimitersToEncode = @":#[]@"; // does not include "?" or "/" due to RFC 3986 - Section 3.4
    static NSString * const kAFCharactersSubDelimitersToEncode = @"!$&'()*+,;=";

    NSMutableCharacterSet * allowedCharacterSet = [[NSCharacterSet URLQueryAllowedCharacterSet] mutableCopy];
    [allowedCharacterSet removeCharactersInString:[kAFCharactersGeneralDelimitersToEncode stringByAppendingString:kAFCharactersSubDelimitersToEncode]];

    // FIXME: https://github.com/AFNetworking/AFNetworking/pull/3028
    // return [string stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacterSet];

    static NSUInteger const batchSize = 50;

    NSUInteger index = 0;
    NSMutableString *escaped = @"".mutableCopy;

    while (index < string.length) {
        NSUInteger length = MIN(string.length - index, batchSize);
        NSRange range = NSMakeRange(index, length);

        // To avoid breaking up character sequences such as 👴🏻👮🏽
        range = [string rangeOfComposedCharacterSequencesForRange:range];

        NSString *substring = [string substringWithRange:range];
        NSString *encoded = [substring stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacterSet];
        [escaped appendString:encoded];

        index += range.length;
    }

    return escaped;
}

相关文章

  • gf框架 ghttp使用

    案例中包含以下内容 get请求 get请求携带参数 post请求携带参数 post请求发送xml数据 post请求...

  • 实现异步请求的方法

    原生ajax写法: 请求方式:get,post,head,delete,get和post的区别 get将请求的参数...

  • 装饰器-验证API入参

    这里主要验证GET请求和POST请求 GET传参 GET把参数包含在URL中 POST传参 POST参数放到bod...

  • iOS-原生网络请求(详细说明)

    技 术 文 章 / 超 人 GET,POST基本带参数网络请求 拼接parameters 参数 上传文件原生请求 ...

  • WebView post传参用法

    iOS WebView Post传参用法(注意URLEncoding编码)UIWebView用法post请求传参数...

  • 接收请求参数和自定类型转换器

    一、接收请求参数 接收请求参数,按照参数的类型可以分为两种:1、采用基本类型接收请求参数(包含get/post)接...

  • 【为了尊严】爬虫(一)

    一、请求 1.GET&POST GET请求中的参数包含在URL里面,数据可以在URL中看到,而POST请求的URL...

  • iOS-三方库-AFNetworking

    一. AFN简介 1. HTTP请求解决方案 在iOS中,常见的发送HTTP请求(GET和POST)的解决方案有:...

  • 2018-10-19——请求方法

    常见的请求方法有两种:GET和POST GET和POST请求方法有如下区别: ①GET请求中的参数包含着URL里面...

  • Gin-获取POST请求参数

    有默认值方式获取POST请求参数 无默认值方式获取POST请求参数 获取POST请求参数的同时,返回参数获取状态,...

网友评论

      本文标题:iOS原生请求 post 参数包含 & 失败的解决方案

      本文链接:https://www.haomeiwen.com/subject/rmrbrdtx.html