WebClient DownloadString Error: “The underlying connection was closed: An unexpected error occurred on a send.”

StackOverflow https://stackoverflow.com/questions/8144239

Question

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!

Was it helpful?

Solution 2

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.

OTHER TIPS

I think you want to use the UploadValues method to make a form 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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top