Solved

Multiple httpwebrequests into response stream loop?  Difficult

Posted on 2013-06-25
4
368 Views
Last Modified: 2013-06-25
I AM doing an httpwebrequest POST to a url

What I need to do is multiple posts and do a single response stream if possible.

In my code below the response is kept "open" with the While "Canread" method.

The postdata goes in like this...but I can only send 7 at a time in the same POST
I NEED to send 7 at a time with a 3 second pause between posts"
<command><id>123</id></command>
<command><id>124</id></command>
<command><id>125</id></command>
<command><id>126</id></command>


And again...after the posts above run, I need to montor all the events

Dim sendWebRequest As HttpWebRequest = CType(WebRequest.Create(stUrl), HttpWebRequest)

sendWebRequest.SendChunked = True
sendWebRequest.Method = "POST"
sendWebRequest.ContentType = "text/plain"
sendWebRequest.KeepAlive = True
sendWebRequest.UserAgent = "CometTest"

'PUT A LOOP AROUNT THE getbytes???
Dim encodedData As New ASCIIEncoding()
Dim byteArray As Byte() = encodedData.GetBytes(postData)
sendWebRequest.ContentLength = byteArray.Length

Dim newStream As Stream = sendWebRequest.GetRequestStream()
newStream.Write(byteArray, 0, byteArray.Length)
newStream.Close()

Using myHttpWebResponse As HttpWebResponse = CType(sendWebRequest.GetResponse(), HttpWebResponse)

    Using streamResponse As Stream = myHttpWebResponse.GetResponseStream()

        'Next Line tricks into staying in Response Stream Loop
        While streamResponse.CanRead

Open in new window


              etc...
        End While
0
Comment
Question by:lrbrister
  • 2
  • 2
4 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39274638
Modify following section

Dim newStream As Stream = sendWebRequest.GetRequestStream()
newStream.Write(byteArray, 0, byteArray.Length)
Thread.Sleep(4000)
newStream.Write(byteArray2, 0, byteArray2.Length)
Thread.Sleep(4000)
newStream.Write(byteArray3, 0, byteArray3.Length)
Thread.Sleep(4000)
...
newStream.Close()



You may want to utilize a loop.
0
 

Author Comment

by:lrbrister
ID: 39274676
CodeCruiser,
  How would that loop look?

What would it be around?

NOt sure where the byteArray2, 3 etc is coming form.
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 39274728
bytearray2, 3 etc are created by breaking your original xml to segments of 7 phones and then converting to byte array. Loop may look like

For i as integer = 0 to PhoneCount/7 - 1
      XmlString = GetXmlString(OriginalXML, StartIndex)
      ByteArray = GetByteArray(XmlString)
      newStream.Write(byteArray, 0, byteArray.Length)
      Thread.Sleep(4000)
Next


Note: Above is  just an algorithm not actual code.
0
 

Author Closing Comment

by:lrbrister
ID: 39274949
Thanks.
Great example and I can get through this.
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

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…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

914 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

19 Experts available now in Live!

Get 1:1 Help Now