如何使用流阅读器vb.net从httpresponse读取时跳过一行

时间:2014-10-26 12:54:47

标签: vb.net

Dim myReq As System.Net.HttpWebRequest =     System.Net.WebRequest.Create("http://google.com/finance/historical?q=tadawul:" & SymbolName & "&enddate=" & Microsoft.VisualBasic.DateAndTime.MonthName(EndDate.Month, True) & "+" + CStr(EndDate.Day) + "+" + CStr(EndDate.Year) + "&startdate=" &  Microsoft.VisualBasic.DateAndTime.MonthName(StartDate.Month, True) & "+" & CStr(StartDate.Day) & "+" +   CStr(StartDate.Year) + "&output=csv")

        Dim wres As System.Net.HttpWebResponse = myReq.GetResponse
        Dim sr As New IO.StreamReader(wres.GetResponseStream)
        sr.BaseStream.Seek(0, SeekOrigin.Begin)
        While sr.Peek() > -1
            .......... some code needed here
        End While
        sr.Close()

我正在阅读此链接 google finance historical price

我正在阅读的数据如下:

Date    Open    High    Low Close   Volume
Oct 23, 2014    45.50   45.60   45.00   45.11   0
Oct 22, 2014    45.40   46.40   44.80   46.14   0
Oct 21, 2014    43.50   45.20   43.50   45.11   0
Oct 20, 2014      -       -     43.20   43.68   0  <--------- I want to skip  This line while reading
Oct 19, 2014    45.50   45.90   44.20   44.44   0
Oct 16, 2014    46.30   46.30   43.00   43.71   0
Oct 15, 2014    48.10   47.80   47.00   47.00   0
Oct 14, 2014    47.50   48.50   46.50   48.17   0

问题是我想跳过带有破折号( - )的行作为数据。 我正在使用vb.net。 任何帮助

2 个答案:

答案 0 :(得分:2)

我通常做这样的事情:

Dim wres As System.Net.HttpWebResponse = myReq.GetResponse
    Dim sr As New IO.StreamReader(wres.GetResponseStream)
    sr.BaseStream.Seek(0, SeekOrigin.Begin)
    'temporarily hold string
    Dim strTempline As String
    While sr.Peek() > -1
        strTempline = sr.ReadLine()
        'just check for the presence of a dash
        If strTempline.Contains("-") Then
            'do nothing
        Else
            'Do something
        End If

    End While
    sr.Close()

(基本上我正在阅读每一行并丢弃我不需要的那些)

答案 1 :(得分:0)

我认为这会有所帮助

Dim str As String = sr.ReadToEnd
        '//MsgBox(str)
        If str.Contains("-,") Then
            flag = True
        End If
        wres.Close()
        Debug.Write("Data:")
        Debug.Write(str)
        Dim j As Integer
        j = 1

        Data = Split(Replace(str, Chr(13), "", 1, -1, CompareMethod.Binary), Chr(10))

        If flag = True Then
            Dim rr As String = ""
            For r As Integer = Data.GetLowerBound(0) To Data.GetUpperBound(0)

                Dim open As String
                Dim high As String
                Dim low As String
                Dim theDate As String
                Dim close As String
                Dim volume As String
                Dim array() As String = Data(r).Split(",")
                open = array(1)
                high = array(2)
                low = array(3)
                close = array(4)
                volume = array(5)
                theDate = array(0)
                If open = "-" And low = "-" And high <> "-" Then

                    open = high
                    low = high
                    Data(r) = theDate + "," + open + "," + high + "," + low + "," + close + "," + volume
                    'rr = rr + Data(r) + Environment.NewLine
                    Exit For

                End If

                If open = "-" And low = "-" And high = "-" And close <> "-" Then
                    open = close
                    low = close
                    high = close
                    Data(r) = theDate + "," + open + "," + high + "," + low + "," + close + "," + volume
                    'rr = rr + Data(r) + Environment.NewLine
                    Exit For

                End If


                If open = "-" Then
                    If high = "-" Then
                        If low = "-" Then
                            open = close
                            high = close
                            low = close
                            Data(r) = theDate + "," + open + "," + high + "," + low + "," + close + "," + volume
                            'rr = rr + Data(r) + Environment.NewLine
                            Exit For
                        Else
                            open = low
                            high = low
                            Data(r) = theDate + "," + open + "," + high + "," + low + "," + close + "," + volume
                            'rr = rr + Data(r) + Environment.NewLine
                            Exit For
                        End If
                        open = high
                        Data(r) = theDate + "," + open + "," + high + "," + low + "," + close + "," + volume
                        'rr = rr + Data(r) + Environment.NewLine
                        Exit For

                    Else
                        open = high
                        Data(r) = theDate + "," + open + "," + high + "," + low + "," + close + "," + volume
                        'rr = rr + Data(r) + Environment.NewLine
                        Exit For
                    End If


                End If
            Next

            str = ""
            For s As Integer = 0 To Data.Length - 1 Step 1
                str = str + Data(s) + Environment.NewLine
            Next
        End If