Solved

http connection type keep alive and chunked

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to open text file 11 69
Data is not showing from images 15 37
Javascript and Jquery not firing 9 39
Point to Current Row Ater Refresh Datagridview 3 20
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 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

15 Experts available now in Live!

Get 1:1 Help Now