加载webView发送请求

  [self loadWebView:_webView withURLString:@"http://sjkdslfskdlnkndsnk" andPostDictionaryOrNil:_para];


- (void) loadWebView:(UIWebView *)theWebView withURLString:(NSString *)urlString andPostDictionaryOrNil:(NSDictionary *)postDictionary

{

    NSURL *url = [NSURL URLWithString:urlString];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url

                                                           cachePolicy:NSURLRequestReloadIgnoringCacheData

                                                       timeoutInterval:60.0];

    

    // DATA TO POST

    if(postDictionary) {

        NSString *postString = [self getFormDataString:postDictionary];

        NSData *postData = [postString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

        

        

        [request setHTTPMethod:@"POST"];

        

        //       [request setValue:@"text/html;charset=utf-8" forHTTPHeaderField:@"Content­-Type"];

        [request addValue:@"IOS" forHTTPHeaderField:@"YUFU-TERMINAL-TYPE"];

        [request addValue:@"1.0.1" forHTTPHeaderField:@"YUFU-TERMINAL-VERSION"];

        [request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

        [request setHTTPBody:postData];

    }

    

    [theWebView loadRequest:request];

}

- (NSString *)getFormDataString:(NSDictionary*)dictionary {

    if( ! dictionary) {

        return nil;

    }

    NSArray* keys = [dictionary allKeys];

    NSMutableString* resultString = [[NSMutableString alloc] init];

    for (int i = 0; i < [keys count]; i++) {

        NSString *key = [NSString stringWithFormat:@"%@", [keys objectAtIndex: i]];

        NSString *value = [NSString stringWithFormat:@"%@", [dictionary valueForKey: [keys objectAtIndex: i]]];

        

        NSString *encodedKey = [self escapeString:key];

        NSString *encodedValue = [self escapeString:value];

        

        NSString *kvPair = [NSString stringWithFormat:@"%@=%@", encodedKey, encodedValue];

        if(i > 0) {

            [resultString appendString:@"&"];

        }

        [resultString appendString:kvPair];

    }

    return resultString;

}

//对key和Value的数据进行 编码处理

- (NSString *)escapeString:(NSString *)string {

    if(string == nil || [string isEqualToString:@""]) {

        return @"";

    }

    NSString *outString = [NSString stringWithString:string];

    outString = [outString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    

    // BUG IN stringByAddingPercentEscapesUsingEncoding

    // WE NEED TO DO several OURSELVES

    outString = [self replace:outString lookFor:@"&" replaceWith:@"%26"];

    outString = [self replace:outString lookFor:@"?" replaceWith:@"%3F"];

    outString = [self replace:outString lookFor:@"=" replaceWith:@"%3D"];

    outString = [self replace:outString lookFor:@"+" replaceWith:@"%2B"];

    outString = [self replace:outString lookFor:@";" replaceWith:@"%3B"];

    

    return outString;

}


//去掉空格和特殊字符

- (NSString *)replace:(NSString *)originalString lookFor:(NSString *)find replaceWith:(NSString *)replaceWith {

    if ( ! originalString || ! find) {

        return originalString;

    }

    

    if( ! replaceWith) {

        replaceWith = @"";

    }

    

    NSMutableString *mstring = [NSMutableString stringWithString:originalString];

    NSRange wholeShebang = NSMakeRange(0, [originalString length]);

    

    [mstring replaceOccurrencesOfString: find

                             withString: replaceWith

                                options: 0

                                  range: wholeShebang];

    

    return [NSString stringWithString: mstring];

}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值