2011-11-15 212 views
0

我有下面的代碼,我試圖檢索HTML文檔。不知道爲什麼它不起作用。WebClient DownloadString錯誤:「底層連接已關閉:發送時發生意外錯誤。」

這是我從現場HTTP頭捕獲:

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

,這裏是我使用的代碼:

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

回答這個問題是在另外一個問題,我問here.

回答原來我需要保留餅乾爲了得到它的工作。並且做得有點不同。

1

我想你想使用UploadValues方法,使表單POST:

// 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); 
+0

我給了一個鏡頭,它沒有奏效。我把它放在我原來的文章中的代碼中,這樣你就可以看到我做了什麼。對不起,代碼看起來有多混亂。 – Jon49

+0

這是我做的第三次嘗試,順便說一句。 – Jon49

相關問題