Solved

http connection type keep alive and chunked

Posted on 2013-05-23
4
336 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now