Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

http connection type keep alive and chunked

Posted on 2013-05-23
4
362 Views
Last Modified: 2013-05-24
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
Comment
Question by:lrbrister
  • 2
  • 2
4 Comments
 
LVL 11

Expert Comment

by:madgino
ID: 39194271
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
 

Author Comment

by:lrbrister
ID: 39194305
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
 
LVL 11

Accepted Solution

by:
madgino earned 500 total points
ID: 39194790
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
 

Author Closing Comment

by:lrbrister
ID: 39194818
Thanks!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question