55

是否可以通过带有 POST 参数的 UIWebView 加载页面?我可能可以只加载带有参数的嵌入式表单并用javascript填充它们并强制提交,但是有没有更清洁和更快的方法?

谢谢!

4

5 回答 5

120

创建 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];
于 2009-07-17T12:37:00.850 回答
16

以下是内容类型为 x-www-form-urlencoded 的 Web 视图的 POST 调用示例。

您需要更改其他内容类型的 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)
于 2016-02-17T15:47:20.127 回答
7

来自oxigen的答案做了一个小的改动。使用时:

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

当向它工作的URL添加一个结束斜杠时,它不起作用,无论是作为 GET 还是 POST 请求。

NSString *theURL = @"http://your_url.com/sub/";
于 2012-10-30T15:12:04.137 回答
1

这是 Swift 3 的 @2ank3th 的修改答案:

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)
于 2017-05-16T17:10:22.173 回答
1

快速 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)
于 2018-02-28T12:25:13.953 回答