-
ios - Session requestiOS 2021. 2. 5. 12:20
Session request
func postSession() { print("현재 사이트 : \(Singleton.instance.currentSite)") print("현재 innerText : \(Singleton.instance.innerText)") print("현재 innerHtml : \(Singleton.instance.innerHTML)") let url = URL(string: Constants.SERVER_TICKET_URL) let param = "URL=\(Singleton.instance.currentURL)&FullContents=\(Singleton.instance.innerText)&MoreInfo=\(Singleton.instance.innerHTML)" let paramData = param.data(using: .utf8) var requestBodyComponents = URLComponents() requestBodyComponents.queryItems = [URLQueryItem(name: "URL", value: Singleton.instance.currentURL) ,URLQueryItem(name: "FullContents", value: Singleton.instance.innerText) ,URLQueryItem(name: "MoreInfo", value: urlEncode(str: Singleton.instance.innerHTML) )] print("URL : " , Singleton.instance.currentURL) print("FullConetents : ", Singleton.instance.innerText) print("MoreInfo : ", Singleton.instance.innerHTML) var request = URLRequest(url: url!) request.httpMethod = "POST" //request.httpBody = paramData request.httpBody = requestBodyComponents.query?.data(using: .utf8) request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type") request.addValue("Bearer \(Singleton.instance.bundleObjectForKey(defaultName: Keys.ACCESSTOKEN))", forHTTPHeaderField: "Authorization") request.setValue(String(paramData!.count), forHTTPHeaderField: "Content-Length") let task = URLSession.shared.dataTask(with: request) { (data, response, error) in // 서버가 응답이 없거나 통신이 실패 if let e = error { NSLog("An error has occured: \(e.localizedDescription)") return } // 응답 처리 로직 print("Response Data=\(String(data: data!, encoding: .utf8)!)") DispatchQueue.main.async() { print("Response Data=\(String(data: data!, encoding: .utf8)!)") print("response = \(response)") let result = (String(data: data!, encoding: .utf8))! if let httpResponse = response as? HTTPURLResponse { print("statusCode \(httpResponse.statusCode)") print("respone : \(httpResponse)") switch httpResponse.statusCode { case 200: let alert = UIAlertController(title: "성공", message: "my page로 이동하시겠습니까?", preferredStyle: UIAlertController.Style.alert) let okAction = UIAlertAction(title: "예", style: .default) { (action) in self.changeWebViewURL(urlStr: "\(Constants.SERVER_BASE_URL)/tickets/my") } let cancelAction = UIAlertAction(title: "아니오", style: .default) { (action) in } alert.addAction(okAction) alert.addAction(cancelAction) self.present(alert, animated: false, completion: nil) default: break } } // 서버로부터 응답된 스트링 표시 } } // POST 전송 task.resume() }
'iOS' 카테고리의 다른 글