How do I insert a POST request into a UIWebView

前端 未结 7 1514
故里飘歌
故里飘歌 2021-02-01 08:50

For a GET request I\'ve tried this simple method:

NSString *urlAddress = @"http://example.com/";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLReq         


        
7条回答
  •  醉酒成梦
    2021-02-01 09:06

    (edited original answer to include newly tested code)

    I just wanted to drop in my version of this request. I have used a dictionary to represent the post parameters.

    It's a chunk of code but is simple enough to drop into a view with a webview and use for all URL loading. It will only POST if you send a "postDictionary". Otherwise it will use the url you sent it to just GET things.

    - (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];
            NSString *postLength                = [NSString stringWithFormat:@"%d", [postData length]];
            [request setHTTPMethod:@"POST"];
            [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
            [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;
    }
    - (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];
    }
    

提交回复
热议问题