web-dev-qa-db-ja.com

POSTパラメーターを使用してUIWebViewを介してWebページをロードする

POST parameters?より速い方法?

ありがとう!

53

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];
117
oxigen

以下はPOSTコンテンツタイプx-www-form-urlencodedのWebビューの呼び出しの例です。

他のコンテンツタイプのpostDataを変更する必要があります。

Swiftの場合

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)
13
2ank3th

oxygenからの回答はわずかな変更で機能しました。使用する場合:

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

GETまたはPOSTリクエスト)として機能しませんでした。末尾のスラッシュをtheURLに追加すると機能しました。

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

これは、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)

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
    }

}

この拡張機能は、パラメーターとその値を処理するのに非常に便利です。 2番目:

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)
0
iman kazemayni