使用POST参数通过UIWebView加载网页

时间:2009-07-17 10:47:27

标签: iphone http uiwebview

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

谢谢!

5 个答案:

答案 0 :(得分:115)

创建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];

答案 1 :(得分:12)

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

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

适用于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)

对于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)

答案 2 :(得分:7)

来自 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/";

答案 3 :(得分: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)

答案 4 :(得分:0)

for swift 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)