HTTP Post和Get下载CSV文件 - 错误

时间:2012-03-27 19:06:32

标签: http post

该页面不断返回登录状态。我验证了凭据是否传递给请求。没运气。请帮忙。我需要在第二天或第二天完成。我在这里失踪了什么?我过去两天已经离开了这个论坛,但却无法理解。对不起,如果这是重复的。

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()

1 个答案:

答案 0 :(得分:0)

NetworkCredential类用于HTTP身份验证。您尝试登录的应用程序使用基于表单的身份验证。

不使用HTTP身份验证,只需使用登录名和密码构建一个urlencoded POST请求。登录表单中还有一个隐藏字段可能很重要。

看起来您正在尝试从提供数据的公司提供适当的数据Feed产品。这可能是一个更好的选择。