Mohammad Hammadi
Mohammad Hammadi

Reputation: 793

how to skip a line while reading from httpresponse using stream reader 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()

I am reading from this link google finance historical price

Data I am reading are as follow:

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

The problem is that I wanna skip lines that have dashes (-) as data. I am using vb.net . Any Help

Upvotes: 0

Views: 293

Answers (2)

Mohammad Hammadi
Mohammad Hammadi

Reputation: 793

I think this will help

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

Upvotes: 0

Yosem
Yosem

Reputation: 4765

I usually do something like this:

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()

(basically I am reading each line and throwing out the ones I don't need)

Upvotes: 2

Related Questions