网页POST,银联

解决思路

(后记,昨天突然银联不行了 出现“invalid request”,各种修改各种失败,最后用线上的环境,竟然没问题,再次记录一下,修改证书应该也可以)
今天对接银联,需要以post的方式把网页加载上去,设置好之后,进行utf8转化,网上找的方法,各种失败(不能转化/:+),自己进行处理如下

NSURL *url = [NSURL URLWithString: response.data[@"requestUrl"]];//这个是后台(从接口获取)提供的
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary: response.data[@"params"]];//提取数据,这个是后台(从接口获取)提供的
NSString *firstKey = dic.allKeys.firstObject;
NSString *body = [NSString stringWithFormat:@"%@=%@",firstKey,dic[firstKey]];
[dic removeObjectForKey:firstKey];
        for (NSString *keyStr in dic.allKeys) {
            body = [NSString stringWithFormat:@"%@&%@=%@",body,keyStr,dic[keyStr]];
            [dic removeObjectForKey:keyStr];
        }
NSMutableURLRequest* request = [[NSMutableURLRequest alloc]initWithURL:url];
body = [self encodeString:body];
NSData *data = [body dataUsingEncoding: kCFStringEncodingUTF8];
[request setHTTPBody:data];
 [request setHTTPMethod:@"POST"];
 UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.frame];
 [webView loadRequest: request];
 webView.scalesPageToFit =YES;
 webView.delegate = self;
 [self.view addSubview:webView];
-(NSString*)encodeString:(NSString*)unencodedString{
    
    // CharactersToBeEscaped = @":/?&=;+!@#$()~',*";
    
    // CharactersToLeaveUnescaped = @"[].";
    
    NSString*encodedString=(NSString*)
    
//    CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
//                                                              
//                                                              (CFStringRef)unencodedString,
//                                                              
//                                                              NULL,
//                                                              
//                                                              (CFStringRef)@"!*'();:@&=+$,/?%#[]",
//                                                              
//                                                              kCFStringEncodingUTF8));

    CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                              
                                                              (CFStringRef)unencodedString,
                                                              
                                                              NULL,
                                                              
                                                              (CFStringRef)@":/+",
                                                              
                                                              kCFStringEncodingUTF8));

    return encodedString;
    
}

你可能感兴趣的:(网页POST,银联)