iPhoneであるサイトにデータを送信し、結果を取得し配列に保存したいです。
対象サイトは
https://licenseif.mhlw.go.jp/search_isei/ の左側の一般向けの方です。
htmlソースコードより、frameを用いていることを理解し、
実際の表示ページは
-> https://licenseif.mhlw.go.jp/search_isei/jsp/top.jsp
POSTされる先は
-> https://licenseif.mhlw.go.jp/search_isei/jsp/search.do
と判明しています。
POSTされる中身も、
- syokusyu
- seibetsu
- name
- expireKey/
の4つと理解しました。
そこで下記のようなテストコードを作りました。
storyboardではNavigationControllerにembedし、
右にUIBarButtonを配置し、実行ボタンをつけました。
Swift
1// ViewController.swift 2import UIKit 3import WebKit 4 5class ViewController: UIViewController, WKNavigationDelegate { 6 7 var webView = WKWebView() 8 9 override func viewDidLoad() { 10 super.viewDidLoad() 11 // Do any additional setup after loading the view, typically from a nib. 12 // frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height) 13 self.webView = WKWebView() 14 15 let url0 = URL(string: "https://licenseif.mhlw.go.jp/search_isei/jsp/top.jsp") 16 let myRequest = URLRequest(url: url0!) 17 webView.load(myRequest) 18 19 self.view.addSubview(self.webView) 20 } 21 22 @IBAction func naviBtnPressed(_ sender: Any) { 23 webView.evaluateJavaScript("document.documentElement.outerHTML.toString()", 24 completionHandler: { (html: Any?, error: Error?) in 25 //print(html as! String) 26 let htmlSource = html as! String 27 let search = "<input type=\"hidden\" name=\"expireKey\" value=\"" 28 if let range = htmlSource.range(of: search) { 29 let start = range.upperBound 30 let key = htmlSource[htmlSource.index(start, offsetBy: 0)..<htmlSource.index(start, offsetBy: 32)] 31 print("key:", key) 32 33 let postData1 = "shokushu=1&seibetu=1&name=佐藤 大輔&expireKey=(key)" 34 35 var request = URLRequest(url: URL(string: "https://licenseif.mhlw.go.jp/search_isei/jsp/search.do")!) 36 request.httpMethod = "POST" 37 request.httpBody = postData1.data(using: .utf8) 38 39 let task = URLSession.shared.dataTask(with: request, completionHandler: { 40 (data, response, error) in 41 42 if error != nil { 43 print(error!) 44 return 45 } 46 47 print("response: (response!)") 48 let phpOutput = String(data: data!, encoding: .utf8)! 49 print("php output: (phpOutput)") 50 }) 51 task.resume() 52 } else { 53 print("文字列は見つかりませんでした。") 54 } 55 }) 56 } 57}
しかし、うまく取得できません。
swiftのコードよりも、htmlのFORMのPOSTがうまくできていなく?感じます。
search.doのコードの中身が見えないので、
そもそもPOSTできないって場合もありえますか?
アドバイス等いただけると嬉しいです。
P.S.関係ないですが、結果の氏名を画像で返してくるあたりいやらしいですよね(^_^;)
取得しずらくしているつもりなのかな?
【追記】
肝心の得られた結果を記載していませんでした。
Swift
1key: 1efe6c8cf197023f1641ddf9c91bf92 2response: Optional(<NSHTTPURLResponse: 0x280f515a0> { URL: https://licenseif.mhlw.go.jp/search_isei/jsp/search.do } { Status Code: 200, Headers { 3 "Cache-Control" = ( 4 "no-cache" 5 ); 6 Connection = ( 7 "Keep-Alive" 8 ); 9 "Content-Language" = ( 10 "ja-JP" 11 ); 12 "Content-Length" = ( 13 3420 14 ); 15 "Content-Type" = ( 16 "text/html;charset=Windows-31J" 17 ); 18 Date = ( 19 "Fri, 18 Jan 2019 05:21:20 GMT" 20 ); 21 Expires = ( 22 "Fri, 18 Jan 2019 05:21:20 GMT" 23 ); 24 "Keep-Alive" = ( 25 "timeout=5, max=100" 26 ); 27 Pragma = ( 28 "no-cache" 29 ); 30 "Set-Cookie" = ( 31 "JSESSIONID=D8EBAB81912A6B31218C7C6A4BE3DA96; Path=/search_isei; HttpOnly", 32 "TS0148a66e=01819adb30e42012537fe353de47362bb469da438beb99be39aef16250f355c70dcc11ba00; Path=/", 33 "TS013eb5f3=01819adb304b861905bd8b996036dbf97689182c6641d41ff8eee0131c0c776fd953ef6a92a65e7b3bd9fa491789ade01730b1c3c1; path=/search_isei" 34 ); 35} }) 36data:Optional(3420 bytes) 37(lldb) 38 39// となり、以下の行にエラーが吐かれます。 40let phpOutput = String(data: data!, encoding: .utf8)! 41Thread 11: Fatal error: Unexpectedly found nil while unwrapping an Optional value
dataに3420 bytesもデータがあるのに、nilが見つかるのは、
encodingが間違っているからですか?
"Content-Type" = ("text/html;charset=Windows-31J")
と返答がくることを考えると、utf8ではなく、
windows-31Jでencodeしなきゃいけないと言うことでしょうか?
回答2件
あなたの回答
tips
プレビュー