2012-03-27 16 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请求。登录表单中还有一个隐藏字段,可能很重要。

它也看起来像你试图从中获取数据的公司提供了一个适当的数据馈送产品。这可能是一个更好的选择。