使用ASP.NET RSS中的屏幕抓取的HTML内容 [英] HTML contents using screen scraping in ASP.NET RSS
问题描述
我正在尝试以编程方式访问网页。我有授权帐户以及有效的用户名和密码。但是,我通过使用屏幕抓取传递有效的用户名和密码来遇到访问问题,这样我就无法完成其他步骤。我不知道我的代码哪个部分出了问题。请帮忙。我的代码是
myReq2.Method =POST
myReq2.ContentLength = postData.ToString.Length
myReq2.ContentType =application / x-www-form-urlencoded
myReq2.CookieContainer = cookies
myReq2.KeepAlive = True
Dim sw As System.IO.StreamWriter = New System.IO.StreamWriter(myReq2.GetRequestStream())
sw.Write(postData)
sw.Close()< br $>
Dim myResp2 As System.Net.HttpWebResponse = CType(myReq2.GetResponse(),System.Net.HttpWebResponse)
Dim sr2 As System.IO.StreamReader = New System.IO.StreamReader(myResp2.GetResponseStream())
resp2 = sr2.ReadToEnd
sr2.Close()
私有函数ExtractViewState(ByVal s As String)As String
返回System.Web.HttpUtility.UrlEncodeUnicode(系统.Text.RegularExpressions.Regex.Match(s,(?< = __ VIEWSTATEvalue =)(?< val>。*?)(?=))。组(val) .Value)
结束函数
I am trying to access the webpage programmatically. I do have the authorized account as well as the valid username and password. However,i run into problem of getting the access by passing the valid username and password using screen scraping so that i cannot do the further steps. I don''t know which part of my code goes wrong.Please help.Below is my code
myReq2.Method = "POST"
myReq2.ContentLength = postData.ToString.Length
myReq2.ContentType = "application/x-www-form-urlencoded"
myReq2.CookieContainer = cookies
myReq2.KeepAlive = True
Dim sw As System.IO.StreamWriter = New System.IO.StreamWriter(myReq2.GetRequestStream())
sw.Write(postData)
sw.Close()
Dim myResp2 As System.Net.HttpWebResponse = CType(myReq2.GetResponse(), System.Net.HttpWebResponse)
Dim sr2 As System.IO.StreamReader = New System.IO.StreamReader(myResp2.GetResponseStream())
resp2 = sr2.ReadToEnd
sr2.Close()
Private Function ExtractViewState(ByVal s As String) As String
Return System.Web.HttpUtility.UrlEncodeUnicode(System.Text.RegularExpressions.Regex.Match(s, "(?<=__VIEWSTATE"" value="")(?<val>.*?)(?="")").Groups("val").Value)
End Function
推荐答案
我认为内容如果您发布的流不正确,则需要对字符串进行ASCII编码,然后再将其添加到您的请求中。
请在此处查看对代码的修改:
I think the content of the stream that you post is not correct, you need to ASCII encoded the string before adding it to your request.
See the modifications to your code here:
Dim postString As String
postString = "use...=<>&pass...=<>" ' insert the name-value pairs to post here
Dim postData As Byte() ' you are going to post encoded bytes
postData = Encoding.UTF8.GetBytes(postString) ' convert the string to the UTF8 encoded bytes
Dim myReq2 As System.Net.HttpWebRequest = System.Net.WebRequest.Create(roServerSettings.GET_MSI_URL & "Login.aspx")
myReq2.Method = "POST"
myReq2.ContentLength = postData.Length ' The length of the encoded bytes
myReq2.ContentType = "application/x-www-form-urlencoded"
myReq2.CookieContainer = cookies
myReq2.KeepAlive = True
Dim sw As System.IO.StreamWriter = New System.IO.StreamWriter(myReq2.GetRequestStream())
sw.Write(postData)
sw.Close()
这应该将字符串正确编码到另一侧。
如果仍然出现错误,请检查是否正确发送格式化的名称 - 值对根据API规范。
This should get the string correctly encoded to the other side.
If you still get an error, check, if you are sending correctly formatted name-value pairs as per the API specification.
这篇关于使用ASP.NET RSS中的屏幕抓取的HTML内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!