2009-07-17 16 views

risposta

105

Creare POST URLRequest e utilizzare per riempire 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]; 
+0

Testato e funziona, grazie! –

+0

funziona bene. Cheers –

+7

Penso che la 'richiesta' dovrebbe essere rilasciata alla fine. :) – Kjuly

7

La risposta da ossigeno lavorato con una piccola modifica. Quando si utilizza:

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

Non ha funzionato, né come GET o POST richieste, se aggiunta una barra che termina al Theurl ha funzionato.

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

Questo è molto strano ... Sono sicuro che l'ho provato nel maggio 2012 e ha funzionato senza il /. Ma certamente ha bisogno del/ora, o il POST è misteriosamente trasformato in GET. Grazie! – emrys57

9

Per Swift

Di seguito è riportato un esempio di chiamata POST per la visualizzazione web con tipo di contenuto x-www-form-urlencoded.

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) 

È necessario modificare PostData per altri tipi di contenuto.

1

Questo è modificare risposta @ 2ank3th 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) 
0

una rapida 4

primo è più opportuno utilizzare questa estensione:

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 
    } 

} 

questa estensione è molto utile gestire i parametri e i suoi valori. secondo:

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) 
Problemi correlati