更快的HTTPWEBREQUEST / WEBRESPONSE - 太慢了

时间:2014-03-24 13:45:31

标签: vb.net httpwebrequest httpwebresponse

有没有办法加快速度?它将通过2000年的列表并逐一进行。请注意,我已尝试过"服务管理器最大连接/默认连接等。这些都不是有价值的解决方案。

'
' Created by SharpDevelop.
' User: merickson2
' Date: 3/22/2014
' Time: 5:59 PM
' 
' To change this template use Tools | Options | Coding | Edit Standard Headers.
'
Imports System.Net
Imports System.IO
Imports System.Text
Imports System.Text.RegularExpressions

Public Partial Class MainForm
    Dim Fetch1 As Integer    
    Dim NewList1 As Integer
    Dim SplitList() As String
    Dim tempCookies As New CookieContainer
    Dim encoding As New UTF8Encoding

    Public Sub New()
        ' The Me.InitializeComponent call is required for Windows Forms designer support.
        Me.InitializeComponent()

        '
        ' TODO : Add constructor code after InitializeComponents
        '
    End Sub

    Sub MainFormLoad(sender As Object, e As EventArgs)
        'Do stuff
    End Sub

    Sub Button1Click(sender As Object, e As EventArgs)
        Dim postData As String = "Login Data"
        Dim byteData As Byte() = encoding.GetBytes(postData)
        Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("http://Login"), HttpWebRequest)
        postReq.Method = "POST"
        postReq.KeepAlive = True
        postReq.CookieContainer = tempCookies
        postReq.ContentType = "application/x-www-form-urlencoded"
        postReq.Referer = "http://login
        postReq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.3) Gecko/20100401 Firefox/4.0 (.NET CLR 3.5.30729)"
        postReq.ContentLength = byteData.Length
        Dim postreqstream As Stream = postReq.GetRequestStream()
        postreqstream.Write(byteData, 0, byteData.Length)
        postreqstream.Close
        Dim postresponse As HttpWebResponse
        postresponse = DirectCast(postReq.GetResponse(), HttpWebResponse)
        tempCookies.Add(postresponse.Cookies)
        Dim postreqreader As New StreamReader(postresponse.GetResponseStream())
        Dim thepage As String = postreqreader.ReadToEnd
        InitLeech()
    End Sub

    Public Sub InitLeech()
            For x = 0 To Listbox2.Items.Count - 1
            SplitList = Split(listBox2.Items(x), "|")
            Dim postData2 As String = "Search Data"
            Dim byteData2 As Byte() = encoding.GetBytes(postData2)
            Dim postReq2 As HttpWebRequest = DirectCast(WebRequest.Create("http://Search"), HttpWebRequest)
            postReq2.Method = "POST"
            postReq2.KeepAlive = False
            postReq2.CookieContainer = tempCookies
            postReq2.ContentType = "application/x-www-form-urlencoded"
            postReq2.Referer = "http://Search"
            postReq2.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.3) Gecko/20100401 Firefox/4.0 (.NET CLR 3.5.30729)"
            postReq2.ContentLength = byteData2.Length
            Dim postreqstream2 As Stream = postReq2.GetRequestStream()
            postreqstream2.Write(byteData2, 0, byteData2.Length)
            postreqstream2.Close
            Dim postresponse2 As HttpWebResponse
            postresponse2 = DirectCast(postReq2.GetResponse(), HttpWebResponse)
            Dim postreqreader2 As New StreamReader(postresponse2.GetResponseStream())
            Dim thepage2 As String = postreqreader2.ReadToEnd
            Dim SplitIt() As String
            Dim CheckRating As String
            Dim WrongStuff As String
            If Len(thepage2) > 10 Then
                If InStr(thepage2,"UCDMC:") > 0 then
                    SplitIt = Split(thepage2,"UCDMC:",7)
                    CheckRating = SplitIt(1).Substring(29,2)
                    CheckRating = Replace(CheckRating,".", "")
                    textBox1.Text = checkrating
                    Dim FullName As String
                    Dim TrueName As String
                    Dim DOB As String
                    Dim Sex As String
                    Dim StartP As Integer
                    Dim EndP As Integer
                    Dim Addy As String
                    StartP = InStr(thepage2,"UCDMC:") + 129
                    StartP = InStr(StartP, thepage2, ">")
                    EndP = InStr(StartP, thepage2, "</")
                    FullName = Trim(Strings.Mid(thepage2, StartP, EndP - StartP))
                    FullName = Replace(FullName, ">", "")
                    FullName = Replace(FullName, "  ", " ")
                    TrueName = SplitList(0) + ", " + SplitList(1) + " " + SplitList(2)
                    TrueName = Regex.Replace(TrueName, "\p{C}+", "")
                    FullName = Regex.Replace(FullName, "\p{C}+", "")
                    WrongStuff = ""
                    If Trim(FullName) = Trim(TrueName) Then
                        'do nothing
                    Else
                        WrongStuff = " + (Wrong: Name"
                    End If

                    StartP = EndP + 23
                    EndP = InStr(StartP, thepage2, "</")
                    DOB = Trim(Strings.Mid(thepage2, StartP, EndP - StartP))
                    DOB = Replace(DOB, "<", "")
                    Dim Dobcheck As String
                    Dobcheck = Replace(DOB, "-", "")
                    If Dobcheck = SplitList(3) Then
                        'do nothing
                    Else
                        If WrongStuff = "" Then
                            WrongStuff = " + (Wrong: DOB"    
                        Else
                            WrongStuff = WrongStuff + "/DOB"
                        End If
                    End If

                    StartP = EndP + 23
                    EndP = InStr(StartP, thepage2, "-")
                    Sex = Trim(Strings.Mid(thepage2, StartP, EndP - StartP))
                    Sex = Replace(Sex, "<", "")
                    If Sex = SplitList(4) Then
                        'do nothing
                    Else
                        If WrongStuff = "" Then
                            WrongStuff = " + (Wrong: SEX"    
                        Else
                            WrongStuff = WrongStuff + "/SEX"
                        End If
                    End If
                    StartP = EndP + 62
                    EndP = InStr(StartP, thepage2, ",")
                    Addy = Trim(Strings.Mid(thepage2, StartP, EndP - StartP))
                    Addy = Replace(Addy, "<BR>", " - ")
                    Addy = Replace(Addy, Chr(34), "")
                    Addy = Replace(Addy, ">", "")
                    If InStr(Addy, "/td") > 0 Then
                        Addy = "No Address Given"
                    End If
                    If WrongStuff = "" Then
                        'do nothing
                    Else
                        WrongStuff = WrongStuff + ")"
                    End If
                    If checkBox1.Checked = True Then
                        WrongStuff = WrongStuff + " + {" + listBox2.Items(x).ToString + "}"
                    End If
                    If CheckRating > 6 then
                        If SplitList(2) = "" Then                                
                            listBox1.Items.Add("[" + SplitList(0) + ", " + SplitList(1) + " * " + SplitList(3) + " * " + SplitList(4) + "] + (Weight: " + CheckRating + ")" + " + (MRN: " + SplitIt(1).Substring(0,7) + ") + [" + FullName + " * " + Dobcheck + " * " + Sex + "] + {" + Addy + "}" + WrongStuff)
                        Else
                            listBox1.Items.Add("[" + SplitList(0) + ", " + SplitList(1) + " " + SplitList(2) +  " * " + SplitList(3) + " * " + SplitList(4) + "] + (Weight: " + CheckRating + ")" + " + (MRN: " + SplitIt(1).Substring(0,7) + ") + [" + FullName + " * " + Dobcheck + " * " + Sex + "] + {" + Addy + "}" + WrongStuff)
                        End If
                        label2.Text = "Existing Patients: " + listBox1.Items.Count.ToString
                    Else
                        If SplitList(2) = "" Then                                
                            listBox3.Items.Add("[" + SplitList(0) + ", " + SplitList(1) + " * " + SplitList(3) + " * " + SplitList(4) + "] + (Weight: " + CheckRating + ")" + " + (MRN: " + SplitIt(1).Substring(0,7) + ") + [" + FullName + " * " + Dobcheck + " * " + Sex + "] + {" + Addy + "}" + WrongStuff)
                        Else
                            listBox3.Items.Add("[" + SplitList(0) + ", " + SplitList(1) + " " + SplitList(2) +  " * " + SplitList(3) + " * " + SplitList(4) + "] + (Weight: " + CheckRating + ")" + " + (MRN: " + SplitIt(1).Substring(0,7) + ") + [" + FullName + " * " + Dobcheck + " * " + Sex + "] + {" + Addy + "}" + WrongStuff)
                        End If
                        label3.Text = "New Patients: " + listBox3.Items.Count.ToString
                    End if
                Else
                    If checkBox1.Checked = True Then
                        WrongStuff = " + {" + listBox2.Items(x).ToString + "}"
                    End If
                    listBox3.Items.Add(SplitList(0) + ", " + SplitList(1) + " + (Not Found)" + WrongStuff)
                    label3.Text = "New Patients: " + listBox3.Items.Count.ToString
                End If    
            End If
            label1.Text = "Checking " & listBox1.Items.Count + listBox3.Items.Count & " of " & listBox2.Items.Count.ToString
            fetch1 = fetch1 + 1
            Application.DoEvents()
        Next
        If fetch1.ToString = test1.Text Then
            If listBox1.Items.Count + listBox3.Items.Count = listBox2.Items.Count Then
                label1.Text = "Mission Complete"
                label1.ForeColor = Color.Green    
            Else
                label1.Text = "Checking " & listBox1.Items.Count + listBox3.Items.Count & " of " & listBox2.Items.Count.ToString
            End If    
        Else                
            fetch1 = fetch1 + 1
        End If
    End Sub


    Sub Button2Click(sender As Object, e As EventArgs)
        Dim TempName As String
        Dim TempPath As String
        Dim PCount As Integer
        PCount = listBox2.Items.Count
        Using dialog As New OpenFileDialog
            dialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
            If dialog.ShowDialog() <> DialogResult.OK Then Return
            Dim a As String = My.Computer.FileSystem.ReadAllText(dialog.FileName)
            listBox2.Items.AddRange(IO.File.ReadAllText(dialog.filename).Split(New String() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries))
            TempName = System.IO.Path.GetFileNameWithoutExtension(dialog.FileName) + ".txt"
            TempPath = dialog.FileName
        End Using        
        listBox2.SelectedIndex = 0
        PCount = listBox2.Items.Count - PCount
        richTextBox1.Text = richTextBox1.Text + TempName + ": " + PCount.ToString + vbCrLf
        test0.Text = listBox2.Items.Count.ToString
        NewList1 = NewList1 + 1
        label5.Text = "Patient Files Loaded : " + NewList1.ToString    
End If
    End Sub

End Class

0 个答案:

没有答案