2012-03-27 31 views
1

頁面不斷重新登錄。我驗證了憑據已傳遞到請求。沒有運氣。請幫忙。我需要在接下來的一兩天內完成。我在這裏失蹤的任何東西?過去兩天我已經去過這個論壇,但無法想象它。對不起,如果這是重複的。HTTP發佈並獲取以下載CSV文件 - 錯誤

Dim response As Net.HttpWebResponse 

Dim myURL1 As String 

Dim response As Net.HttpWebResponse 

Dim myURL1 As String 

Dim myURL2 As String 

Dim myURL3 As String 

Dim cookieJar As New CookieContainer 

Dim myReq As Net.HttpWebRequest 

Dim strngData as String 

' Network Credentials 

Dim cred As New System.Net.NetworkCredential ("myLogin", "myPassword", ".genscape.com") 

' Login Page URL 

myURL1 = https://apps.genscape.com/NAPowerRT/login.jsp 
'URL to scrape data 

myURL2 = https://apps.genscape.com/NAPowerRT/planthistory.do? 
plantid=9976&numDays=1&format=0/genscape-03_26_12-03_27_12-9708.csv 

' Action URL in Form with method = POST 

myURL3 = "https://apps.genscape.com/NAPowerRT/j_spring_security_check" 

myReq = Net.HttpWebRequest.Create(myURL1) 

myCache.GetCredential(myURL1, 0, "http") 

myReq.Credentials = cred 



'Get Cookies 

myURL1 = "https://apps.genscape.com/NAPowerRT/login.jsp" 

myReq = Net.HttpWebRequest.Create(myURL1) 

myReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)" 

myReq.Method = "GET" 

myReq.CookieContainer = cookieJar 

myReq.KeepAlive = True 

response = myReq.GetResponse 

For Each tempCookie As Net.Cookie In response.Cookies 

    cookieJar.Add(tempCookie) 

Next 

myReq.GetResponse.Close() 

'Sent POST data 

myReq = Net.HttpWebRequest.Create(myURL3) 

myReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)" 

myReq.Method = "Post" 

myReq.AllowAutoRedirect = True 

myReq.ContentType = "text/html" 

myReq.Credentials = cred 

myReq.CookieContainer = cookieJar 


'Send request to URL that has data with cookies from previous GETs 
' ----- The below results in login page instead of page where the csv is located 

myReq = Net.HttpWebRequest.Create(myURL2) 

myReq.Credentials = cred 

myReq.Method = "get" 

myReq.ContentType = "text/csv" 

myReq.Accept = "text/html" 

myReq.AllowAutoRedirect = True 

myReq.KeepAlive = True 

myReq.CookieContainer = cookieJar 

'Get the data from the page 

response = myReq.GetResponse 

Dim streamreponse As Stream = response.GetResponseStream 

Dim stream As StreamReader = New StreamReader(response.GetResponseStream()) 

strngData = stream.ReadToEnd() 

response.Close() 

回答

0

NetworkCredential類用於HTTP身份驗證。您嘗試登錄的應用使用基於表單的身份驗證。

而不是使用HTTP身份驗證,只需使用登錄名和密碼構建一個urlencoded POST請求。登錄表單中還有一個隱藏字段,可能很重要。

它也看起來像你試圖從中獲取數據的公司提供了一個適當的數據饋送產品。這可能是一個更好的選擇。