我有以下代码,我正在尝试检索HTML文档。不知道为什么它不起作用。
以下是我从Live HTTP标头中捕获的内容:
request#POST https://taylor.yc.edu/BANPROD/pkgyc_yccsweb.P_Results POST /BANPROD/pkgyc_yccsweb.P_Results
term_code = 201130&安培; SEARCH_TYPE = A和关键字=安培; kw_scope =所有&安培; kw_opt =所有&安培; subj_code = BIO&安培; crse_numb = 205安培;校园= &安培;讲师= &安培; instr_session = *&安培; attr_type = *&安培;星期一= ON&安培;周二= ON&安培;星期三= ON&安培;周四= ON&安培;周五= ON&安培;饱和= ON&安培;太阳= ON&安培; avail_flag =上
这是我正在使用的代码:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
mWC = New WebClient
wbParameters = New NameValueCollection
wbParameters.Add("term_code", "201130")
wbParameters.Add("search_type", "A")
wbParameters.Add("keyword", "")
wbParameters.Add("kw_scope", "all")
wbParameters.Add("kw_opt", "all")
wbParameters.Add("subj_code", "BIO")
wbParameters.Add("crse_numb", "205")
wbParameters.Add("campus", "*")
wbParameters.Add("instructor", "*")
wbParameters.Add("instr_session", "*")
wbParameters.Add("attr_type", "*")
wbParameters.Add("mon", "on")
wbParameters.Add("tue", "on")
wbParameters.Add("wed", "on")
wbParameters.Add("thu", "on")
wbParameters.Add("fri", "on")
wbParameters.Add("sat", "on")
wbParameters.Add("sun", "on")
wbParameters.Add("avail_flag", "on")
mWC.QueryString = wbParameters
Try
mWC.Headers(HttpRequestHeader.UserAgent) = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.0.3705; Media Center PC 4.0; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"
mWC.Headers(HttpRequestHeader.AcceptEncoding) = "gzip, deflate"
mWC.Headers(HttpRequestHeader.Accept) = "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/xaml+xml, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-xpsdocument, */*"
mWC.Headers(HttpRequestHeader.KeepAlive) = False
Try
Dim sResult As String = mWC.DownloadString(New Uri("https://taylor.yc.edu/BANPROD/pkgyc_yccsweb.P_Results"))
MsgBox(sResult)
Catch ex As Exception
MsgBox(ex.InnerException.Message)
Try
Dim sResult As String = mWC.DownloadString(New Uri("https://taylor.yc.edu/BANPROD/pkgyc_yccsweb.P_Search"))
MsgBox(sResult)
Catch ex2 As Exception
MsgBox(ex.Message)
Try
Dim bytResults() As Byte = mWC.UploadValues(New Uri("https://taylor.yc.edu/BANPROD/pkgyc_yccsweb.P_Results"), wbParameters)
Catch ex3 As Exception
MsgBox("Didn't work " & ex3.Message)
End Try
End Try
End Try
Catch ex As Exception
My.Computer.Clipboard.SetText(ex.Message)
MsgBox(ex.Message)
End Try
Try
Dim wbr As HttpWebRequest = WebRequest.Create("https://taylor.yc.edu/BANPROD/pkgyc_yccsweb.P_Results")
wbr.KeepAlive = False
wbr.Timeout = 600000
wbr.ReadWriteTimeout = 600000
wbr.Method = "POST"
wbr.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.0.3705; Media Center PC 4.0; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"
wbr.CookieContainer = New CookieContainer()
'wbr.ContentLength = formData.Length
'Dim requestStream As Stream = wbr.GetRequestStream()
'MsgBox(requestStream.Read)
'requestStream.Close()
MsgBox(wbr.GetResponse().ToString)
Catch ex As Exception
MsgBox(ex.Message)
End Try
感谢您的帮助!我从来没有能够让webclient工作,所以这对我来说是个不错的突破!
答案 0 :(得分:1)
我认为您想使用UploadValues方法制作表单帖子:
// Add necessary parameter/value pairs to the name/value container.
myNameValueCollection.Add("Name",name);
myNameValueCollection.Add("Address",address);
myNameValueCollection.Add("Age",age);
// 'The UploadValues(String,NameValueCollection)' implicitly sets HTTP POST as the request method.
byte[] responseArray = myWebClient.UploadValues(uriString,myNameValueCollection);
答案 1 :(得分:0)
我在问here.
的另一个问题中回答了这个问题的答案原来我需要保留cookie才能让它发挥作用。并且做得有点不同。