Loading a webpage through UIWebView with POST para

2019-01-16 07:04发布

问题:

Is it possible to load a page through UIWebView with POST parameters? I can probably just load an embedded form with the parameters and fill them in with javascript and force a submit, but is there a cleaner and faster way?

Thanks!

回答1:

Create POST URLRequest and use it to fill webView

NSURL *url = [NSURL URLWithString: @"http://your_url.com"];
NSString *body = [NSString stringWithFormat: @"arg1=%@&arg2=%@", @"val1",@"val2"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url];
[request setHTTPMethod: @"POST"];
[request setHTTPBody: [body dataUsingEncoding: NSUTF8StringEncoding]];
[webView loadRequest: request];


回答2:

Below are examples for POST call for web view with content type x-www-form-urlencoded.

You need to change postData for other content types.

For Swift 3

let url = NSURL (string: "https://www.google.com")
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "POST"
request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")

let post: String = "sourceId=44574fdsf01e-e4da-4e8c-a897-17722d00e1fe&sourceType=abc"
let postData: NSData = post.dataUsingEncoding(NSASCIIStringEncoding, allowLossyConversion: true)!

request.HTTPBody = postData
webView.loadRequest(request)

For Swift 4

let url = URL (string: "let url = NSURL (string: "https://www.google.com")
let request = NSMutableURLRequest(url: url!)
request.httpMethod = "POST"
request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")

let post: String = "sourceId=44574fdsf01e-e4da-4e8c-a897-17722d00e1fe&sourceType=abc"
let postData: Data = post.data(using: String.Encoding.ascii, allowLossyConversion: true)!

request.httpBody = postData
webView.load(request as URLRequest)


回答3:

The answer from oxigen worked with a minor change. When using:

NSString *theURL = @"http://your_url.com/sub";
...//and later
[request setURL:[NSURL URLWithString:theURL]];

It did not work, neither as GET or POST requests, when added a ending slash to the theURL it worked.

NSString *theURL = @"http://your_url.com/sub/";


回答4:

This is modify answer of @2ank3th for Swift 3:

let url = NSURL (string: "https://www.google.com")
let request = NSMutableURLRequest(url: url! as URL)
request.httpMethod = "POST"
request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")

let post: String = "sourceId=44574fdsf01e-e4da-4e8c-a897-17722d00e1fe&sourceType=abc"
let postData: NSData = post.data(using: String.Encoding.ascii, allowLossyConversion: true)! as NSData

request.httpBody = postData as Data
webView.loadRequest(request as URLRequest)


回答5:

for swift 4

first its better to use this extension :

extension URL{
    func withQueries(_ queries:[String:String]) -> URL? {
        var components = URLComponents(url: self, resolvingAgainstBaseURL: true)
        components?.queryItems = queries.flatMap{URLQueryItem(name: $0.0, value: $0.1)}
        return components?.url
    }

    func justQueries(_ queries:[String:String]) -> URL? {
        var components = URLComponents(url: self, resolvingAgainstBaseURL: true)
        components?.queryItems = queries.flatMap{URLQueryItem(name: $0.0, value: $0.1)}
        return components?.url
    }

}

this extension is very useful to handle parameters and its values. second:

let urlstring = "https://yourdomain.com"
        let url = URL(string: urlstring)!
        let request = NSMutableURLRequest(url: url as URL)
        request.httpMethod = "POST"
        request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
        let query :[String:String] = ["username":"admin","password":"111"]
        let baseurl = URL(string:urlstring)!
        let postString = (baseurl.withQueries(query)!).query
        request.httpBody = postString?.data(using: .utf8)
        webview.load(request as URLRequest)