Help us understand the problem. What is going on with this article?

Swift 2chへの書き込み

Alamofireを使っての2chへの書き込み

    func postTo2ch(_ urlString: String, _ postText: String) {

        let url     = URL(string: urlString)
        let domain  = (url?.host)!  // ドメイン

        let subStrings  = domain.components(separatedBy: ".")
        let server      = subStrings[0] // サーバー

        let paths   = urlString.components(separatedBy: "/")
        let thread  = paths[paths.count-2]  // スレッド
        let board   = paths[paths.count-3]  // 掲示板種別

        let bbsUrl  = "https://\(server).5ch.net/test/bbs.cgi"  // 投稿先CGI

        let headers: HTTPHeaders = [
            "Referer": urlString,
            "Accept-Encoding": "gzip",
            "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
            "Accept-Charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.3",
            "Accept-Language": "en-US,en;q=0.8",
            "Connection": "keep-alive"
        ]
        let parameters: [String: Any] = [
            "bbs": board,
            "key": thread,
            "time": Int(Date().timeIntervalSince1970) - 60,
            "FROM": "",
            "subject": "",
            "mail": "",
            "MESSAGE": postText,
            "submit": "書き込む"
        ]

        Alamofire.request(bbsUrl, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers).responseString { response in
            // 初回アクセス時のみクッキーを設定する
            if response.description.contains("書き込み確認") { 
                let res = response.response
                let cookies = HTTPCookie.cookies(withResponseHeaderFields: res?.allHeaderFields as! [String : String], for: (res?.url!)!)
Alamofire.SessionManager.default.session.configuration.httpCookieStorage?.setCookies(cookies, for: response.response?.url, mainDocumentURL: nil)
                // クッキーを設定してもう一回投稿!
                Alamofire.request(bbsUrl, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers).responseString { response in
                    print(response)
                }
            }
        }
    }

// 使い方
self.postTo2ch("http://awabi.2ch.sc/test/read.cgi/male/1514976917/", "テスト")
  • 改善すべき点
    • 掲示板のURLと書き込みCGIのURLが異なるようで、初回のクッキー保存が一度のアクセスでできなかった
    • 初回アクセス判定をレスポンス内の「書き込み確認」で行なっているのをやめたい
    • URLのパースももっとシンプルにしたい

https://github.com/katafuchix/2chPost

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away