2
發送POST請求要瀏覽的WebKit的WebView的URL是很容易的:如何通過WebKit的
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[[m_webView mainFrame] loadRequest:requestObj];
我看不到的地方/如何POST數據添加到這些對象。如何在Cocoa中使用託管的WebView來執行Post?的回答
示例代碼:
NSData* postData = postString;
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
NSString* contentLength = [NSString stringWithFormat:@"%d", [data length]];
[urlRequest setValue:contentLength forHTTPHeaderField:@"Content-Length"];
[urlRequest setHTTPBody:postData];
[[m_webView mainFrame] loadRequest:urlRequest];