今天帮助朋友调试一个接口
http://180.76.248.109/YWZ/ywz2/sp_ywz2_back_interface?inParameter={"call_name":"sp_tab_user_login","data":{"type":"1","phone_no":"18983766960","passwd":"123456"}}
一看到这个接口很奇怪,因为通常登录请求一般使用post,上面的请求使用的get请求 而且参数是字典嵌套字典,在拼接参数的时候客户端需要做 几次字典转字符串的操作
一开始是很奇怪的
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] init];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
// 设置超时时间
[manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
manager.requestSerializer.timeoutInterval = 10.f;
[manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
[manager.requestSerializer setValue:@"gzip" forHTTPHeaderField:@"Content-Encoding"];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html",@"text/json", @"text/javascript",@"text/plain", nil];
NSMutableDictionary *dict =[NSMutableDictionary dictionary];
[dict setValue:@"sp_tab_user_login" forKey:@"call_name"];
NSMutableDictionary *dataDict =[NSMutableDictionary dictionary];
[dataDict setValue:@"1" forKey:@"type"];
[dataDict setValue:@"18983766960" forKey:@"phone_no"];
[dataDict setValue:@"123456" forKey:@"passwd"];
//这里有可能dataDict要转成String
[dict setValue:dataDict forKey:@"data"];
NSLog(@"参数json字典 %@",dict);
//转成NSData
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:nil];
//转成String
NSString *jsonStr =[[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding ];
NSLog(@"参数json字符串%@",jsonStr);
NSString *baseUrl = @"http://180.76.248.109/YWZ/ywz2/sp_ywz2_back_interface";
NSString *urlStr =[NSString stringWithFormat:@"%@?inParameter=%@",baseUrl,jsonStr];
NSLog(@"请求的urlStr %@",urlStr);
//执行get请求
[manager GET:urlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
// NSLog(@"reponse===> %@",responseObject);
NSJSONSerialization *json =[NSJSONSerialization JSONObjectWithData:(NSData *)responseObject options:0 error:nil];
NSLog(@"json %@",json)
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"error %@",error);
}];
请求发出去后总是失败 报错是这样的
2017-09-02 21:59:52.240921+0800 DDU[2859:741150] *** Assertion failure in -[AFHTTPRequestSerializer requestWithMethod:URLString:parameters:error:], /Users/danny/Desktop/DDUWorkingCopySep01/DDUAug17BackUp/SVNBackUpJune12backup/DDU/DDU/Class/Library/AFNetworking/AFNetworking/AFURLRequestSerialization.m:353
很绝望 然后我说我用苹果自己的NSURLSession试一下吧 然后结果是url格式不支持
Printing description of error:
Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={NSLocalizedDescription=unsupported URL, NSUnderlyingError=0x600000649870 {Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"}}
其实我大概想到是什么问题 应该是字符编码的问题 因为输出url放到浏览器是没问题的 而AFN的断言却告诉我这个url是nil,然后我谷歌了一下 发现 只是加了一行代码就完美解决了
NSString *str1 = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
我觉得应该是我们手动拼接的url 有可能是不是utf8的 二浏览器就比较智能
NSString *str1 = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
Returns a new string made from the receiver by replacing all characters not in the specified set with percent encoded characters.
Entire URL strings cannot be percent-encoded, because each URL component specifies a different set of allowed characters. For example, the query component of a URL allows the “@
” character, but that character must be percent encoded in the password component.
UTF-8 encoding is used to determine the correct percent encoded characters. Any characters in allowedCharacters
outside of the 7-bit ASCII range are ignored.
到此为止 bug完美的修复了