[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];
}