加载网页通过一个UIWebView与POST参数(Loading a webpage through

2019-06-14 19:50发布

是否有可能通过一个UIWebView加载一个页面POST参数? 我可能只需要加载一个嵌入的形式与参数,并用JavaScript填充它们并强制提交,但有一个更清洁,更快捷的方式?

谢谢!

Answer 1:

创建POST的URLRequest并使用它来填充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];


Answer 2:

下面是POST调用用于与内容类型的X WWW窗体-urlencoded幅视图的例子。

您需要更改POSTDATA其他内容类型。

对于斯威夫特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)

对于斯威夫特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)


Answer 3:

oxigen答案对于小的修改工作。 当使用:

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

它没有工作,既不是GET或POST请求,当添加结束斜线,以工作的theURL。

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


Answer 4:

这是修改@ 2ank3th的答案斯威夫特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)


Answer 5:

为迅速4

首先它能够更好地使用这个扩展:

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
    }

}

这个扩展是非常有用的参数处理方式及其价值。 第二:

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)


文章来源: Loading a webpage through UIWebView with POST parameters