2017-06-18 65 views
3

我想創建一個iOS應用程序,該應用程序登錄到website並解析來自該網站多個頁面的數據,同時保持登錄會話。這是我迄今爲止所做的。我發送一個GET請求來檢索POST請求所需的EVENTVALIDATON和VIEWSTATE參數。 (我使用'Firebug'來查看POST)。當我運行下面的代碼時,它會返回相同的登錄頁面。但它應該給我this page如何使用POST請求登錄到網站? (Swift,iOS)

var parameter: Parameters = [:] 
    var viewstate: String = "" 
    var eventvalidation: String = 


    @IBAction func postRequest(_ sender: Any) { 


    Alamofire.request("https://ecampus.psgtech.ac.in/studzone/AttWfLoginPage.aspx").responseString { response in 
     print("\(response.result.isSuccess)") 
     if let html = response.result.value { 
      if let doc = Kanna.HTML(html: html, encoding: String.Encoding.utf8) { 
       // Search for nodes by CSS selector 

       for show in doc.css("input[id='__VIEWSTATE']") { 
        self.viewstate=show["value"]! 
        //print(show["value"] as Any) 
       } 

       for show in doc.css("input[id='__EVENTVALIDATION']") { 
        self.eventvalidation=show["value"]! 
        //print(show["value"] as Any) 
       } 
      } 
     } 
     //creating dictionary for parameters 
     self.parameter = ["__EVENTTARGET":"", 
         "__EVENTARGUMENT":"", 
         "__LASTFOCUS":"", 
         "__VIEWSTATE":self.viewstate, 
         "__EVENTVALIDATION":self.eventvalidation, 
         "rdolst":"S", 
         "Txtstudid":"<myrollno>", 
         "TxtPasswd":"<mypassword>", 
         "btnlogin":"Login" 
     ] 
    } 
    Alamofire.request ("https://ecampus.psgtech.ac.in/studzone/AttWfLoginPage.aspx",method: .post, parameters: self.parameter, headers: headers).responseString { response in 
     print("\(response.result.isSuccess)") 
     print(response) 
    } 

說實話,我對請求和解析數據很陌生(儘管我已經完成了解析部分)。我做了一些更多的研究和了解檢查後頭頭和cookies.So,由瀏覽器最初的GET請求有

Cache-Control : private 
Content-Encoding : gzip 
Content-Length : 4992 
Content-Type : text/html; charset=utf-8 
Date : Sun, 18 Jun 2017 14:25:50 GMT 
Server : Microsoft-IIS/8.0 
Set-Cookie : .ASPXAUTH=; expires=Mon, 11-Oct-1999 18:30:00 GMT; path=/; HttpOnly 
Vary : Accept-Encoding 
X-AspNet-Version : 4.0.30319 
X-Powered-By : ASP.NET 

的響應頭和請求頭的

Accept : text/html,application/xhtml+xml,application/xml;q=0.9;q=0.8 
Accept-Encoding : gzip, deflate, br 
Accept-Language : en-US,en;q=0.5 
Connection : keep-alive 
Cookie : ASP.NET_SessionId=urzugt0zliwkmz3ab1fxx1ja 
Host : ecampus.psgtech.ac.in 
Upgrade-Insecure-Requests : 1 
User-Agent : Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:54.0) Gecko/20100101 Firefox/54.0` 

問題是我不明白如何初始GET請求可以有一個令牌。如果請求首先發生,響應應該是包含令牌的響應?我不知道我做錯了什麼,以及如何使這個工作。我不知道我是否完全錯過了一些東西。我嘗試了所有我能想到的東西之後纔來到這裏。任何幫助,將不勝感激。謝謝。

回答

1

所需EVENTVALIDATON和VIEWSTATE參數POST < -

但在你的代碼的POST請求的GET請求後立即執行,在這一點上self.parameter是空

Alamofire有異步 completionHandler

等待GET請求來完成,然後發送POST請求:

var parameter: Parameters = [:] 
    var viewstate: String = "" 
    var eventvalidation: String = 


@IBAction func postRequest(_ sender: Any) { 


Alamofire.request("https://ecampus.psgtech.ac.in/studzone/AttWfLoginPage.aspx").responseString { response in 
    print("\(response.result.isSuccess)") 
    if let html = response.result.value { 
     if let doc = Kanna.HTML(html: html, encoding: String.Encoding.utf8) { 
      // Search for nodes by CSS selector 

      for show in doc.css("input[id='__VIEWSTATE']") { 
       self.viewstate=show["value"]! 
       //print(show["value"] as Any) 
      } 

      for show in doc.css("input[id='__EVENTVALIDATION']") { 
       self.eventvalidation=show["value"]! 
       //print(show["value"] as Any) 
      } 
     } 
    } 
    //creating dictionary for parameters 
    self.parameter = ["__EVENTTARGET":"", 
        "__EVENTARGUMENT":"", 
        "__LASTFOCUS":"", 
        "__VIEWSTATE":self.viewstate, 
        "__EVENTVALIDATION":self.eventvalidation, 
        "rdolst":"S", 
        "Txtstudid":"15i231", 
        "TxtPasswd":"OpenSesame", 
        "btnlogin":"Login" 
    ] 

    //Wait for the GET request to complete, and then send the POST request: <<== 
    Alamofire.request ("https://ecampus.psgtech.ac.in/studzone/AttWfLoginPage.aspx",method: .post, parameters: self.parameter, headers: headers).responseString { response in 
     print("\(response.result.isSuccess)") 
     print(response) 
    } 

} 
+0

非常感謝。我不知道這些請求是異步的。我將POST請求添加到第一個請求的完成處理程序,並且它已經登錄!謝謝 – Ultimator

+0

我正在爲我必須登錄的網站執行此操作。看起來,我能夠在** POST **之後以正確的參數獲得良好的響應。然後,我使用** ASP.NET_SessionId **發送一個帶** GET **的新請求給另一個頁面,答案告訴我要登錄。我檢查了我發送的內容與瀏覽器完全相同,但沒有運氣。任何想法爲什麼我會得到一個積極的結果,然後立即失敗?在此先感謝 – Chamanhm

+0

在玩了整整一天的問題。 (目前還沒有解決方案):我可以級聯3個Alamofire請求,有時它可以工作,其他時候它會響應HTML請求登錄。我還注意到,如果我使用safari登錄並通過Swift發送ASP.NET_SessionId,它將每次都有效。不知道我在這裏錯過了什麼,但我想通過iOS來完成整個過程。 – Chamanhm