Solved

http connection type keep alive and chunked

Posted on 2013-05-23
4
356 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 those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

832 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