WebClient DownloadString 错误:“底层连接已关闭:发送时发生意外错误。”
我有以下代码,我正在尝试检索 HTML 文档。不知道为什么它不起作用。
这是我从实时 HTTP 标头中捕获的内容:
请求# POST https://taylor.yc.edu/BANPROD/pkgyc_yccsweb.P_Results POST /BANPROD/pkgyc_yccsweb.P_Results
term_code=201130&search_type=A&keyword=&kw_scope=all&kw_opt=all&subj_code=BIO&crse_numb=205&campus=&instru ctor=&instr_session=*&attr_type=*&mon=on&tue=on&wed=on&thu=on&fri=on&sat=on&sun=on&avail_flag=on< /p>
这是我正在使用的代码:
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
感谢您的帮助!我从来没有能够让网络客户端工作,所以这对我来说将是一个很好的突破!
I have the following code that I'm trying to retrieve the HTML document. Not sure why it wouldn't work.
Here's what I captured from Live HTTP Headers:
request# POST https://taylor.yc.edu/BANPROD/pkgyc_yccsweb.P_Results POST /BANPROD/pkgyc_yccsweb.P_Results
term_code=201130&search_type=A&keyword=&kw_scope=all&kw_opt=all&subj_code=BIO&crse_numb=205&campus=&instructor=&instr_session=*&attr_type=*&mon=on&tue=on&wed=on&thu=on&fri=on&sat=on&sun=on&avail_flag=on
and here's the code I'm using:
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
Thanks for your help! I've never been able to get webclient to work so this would be a nice breakthrough for me!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您想使用 UploadValues 方法来发布表单:
I think you want to use the UploadValues method to make a form post:
这个问题的答案在我问的另一个问题中得到了回答这里.
事实证明,我需要保留 cookie 才能使其正常工作。并且做得有点不同。
The answer to this question was answered in another question that I asked here.
Turns out I needed to retain cookies in order to get it to work. And do it quite a bit differently.