Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 395
  • Last Modified:

http connection type keep alive and chunked

I am trying to create a http live connection where KeepAlive = True, Transfer-Encoding = chunked"

My code below works...for the first "chunk"

I'm sure I need a "While" or some kind of Thread or loop in here.

Any ideas?

 Public Sub StartShoreTel(ByVal URI As String, ByVal Parameters As String, kpAlive As Boolean)
        Dim request As HttpWebRequest = Nothing
        Dim response As HttpWebResponse = Nothing

        Try
            ' Create the web request   
            request = DirectCast(WebRequest.Create(URI), HttpWebRequest)
            request.KeepAlive = kpAlive
            request.UserAgent = "CometTest"
            request.SendChunked = True
            request.ContentType = "text/plain"
            request.Method = "POST"

            Dim bytes As Byte() = System.Text.Encoding.ASCII.GetBytes(Parameters)
            request.ContentLength = bytes.Length
            Dim os As System.IO.Stream = request.GetRequestStream()
            os.Write(bytes, 0, bytes.Length)
            os.Close()

            ' Get response   
            response = DirectCast(request.GetResponse(), HttpWebResponse)

            Dim streamResponse As Stream = response.GetResponseStream()
            Dim streamRead As New StreamReader(streamResponse)
            Dim readBuff(20000) As [Char]
            Dim count As Integer = streamRead.Read(readBuff, 0, 20000)
            Dim outputData As New [String](readBuff, 0, count)

            'Send any response to SQL Server
            sendXmlToDatabase(outputData)

            '---Need SOmething Here??
            'While count > 0
            'Dim outputData As New [String](readBuff, 0, count)
            'Console.Write(outputData)
            'End While
            streamRead.Close()
            streamResponse.Close()
            response.Close()
        Finally
            If Not response Is Nothing Then response.Close()
        End Try
    End Sub

Open in new window

0
lrbrister
Asked:
lrbrister
  • 2
  • 2
1 Solution
 
madginoCommented:
Why read only 20000 chars and not read the whole thing?

	
            Dim streamRead As New StreamReader(streamResponse)
            Dim readBuff As String
            Dim readBuff  = streamRead.ReadToEnd()

            'Send any response to SQL Server
            sendXmlToDatabase(readBuff)

Open in new window

0
 
lrbristerAuthor Commented:
Madgino,
Thanks...I didn't catch that.

But my actual question is centered on the "keep alive" so I can send whatever the end page sends me and whenever it is sent.

In other words...this is launched at 8am on MOnday.
I need it to run 24x7 and simply accept whatever is sent.
0
 
madginoCommented:
By default .NET is doing this for you, keep alive is true by default and even if you close the request object, when you create a new one for the same URI the connection is reused.

I couldn't find a msdn link for this but other people are confirming it:

http://forums.asp.net/t/1335623.aspx/1
http://stackoverflow.com/questions/4063930/creating-2-http-requests-on-the-same-connection
0
 
lrbristerAuthor Commented:
Thanks!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now