VB .NET:HTTP POST后无法获得正确的响应

时间:2015-06-30 16:01:26

标签: .net vb.net

我正在尝试从此搜索结果中返回HTML,但我能够获得的是原始页面,其值已插入相应的文本框中。这段代码显然适用于其他人,我不确定问题是什么。

 ' Create a new WebClient instance. 
 Dim myWebClient As New WebClient()
 myWebClient.Headers.Add("user-agent", "Mozilla/5.0 (Windows; Windows NT 5.1; rv:1.9.2.4) Gecko/20100611 Firefox/3.6.4")

 ' Create a new NameValueCollection instance to hold some custom parameters to be posted to the URL. 
 Dim myNameValueCollection As New NameValueCollection()

 ' Add necessary parameter/value pairs to the name/value container.
 myNameValueCollection.Add("conCSLB", "619412")

 ' The Upload(String,NameValueCollection)' method implicitly sets the HTTP POST as the request method.
 Dim responseArray As Byte() = myWebClient.UploadValues("https://efiling.dir.ca.gov/PWCR/Search", myNameValueCollection)

 ' Decode and display the response.
 Dim response As String = Encoding.UTF8.GetString(responseArray)

1 个答案:

答案 0 :(得分:0)

显然,服务器端脚本会检查是否存在其他2个输入元素,即使它们是空的。添加它们:

myNameValueCollection.Add("regNumber", "")
myNameValueCollection.Add("legalName", "")

这解决了您的主要问题,现在您需要解析HTML。但我发现了一件有趣的事情:您只需下载XLS并解析:

My.Computer.Network.DownloadFile("https://efiling.dir.ca.gov/PWCR/Search.action?6578706f7274=1&legalName=&conCSLB=619412&regNumber=&d-49653-e=2", "output.txt")

此文件包含原始数据(不是真正的XLS!):

"Legal Name"    "Registration Number"   "License Type/Number(s)"            "Registration Date" "Expiration Date"
"BLASER BUILDING SPECIALTIES, INC." "1000006374"    "CSLB:619412"   "01/21/2015"    "06/30/2015"

只需更改网址中的参数即可完成