Link to home
Start Free TrialLog in
Avatar of Larry Brister
Larry BristerFlag for United States of America

asked on

Invalid C#?

I saw the following code on StackOverFlow and am tryping to convert it to VB.

Seems like something is seriousely wrong in the middle part.

HttpWebRequest req;

try
{
    req = (HttpWebRequest)WebRequest.Create("http://www.example.com");
    Stream stream = req.GetResponseStream();

    byte[] data = new byte[4096];
    int read;
    while ((read = data.Read(data, 0, data.Length)) > 0)
    {
         Process(data, read);
    }
}
finally
{
    if (req != null)
        req.Close();
}
Avatar of David L. Hansen
David L. Hansen
Flag of United States of America image

Try
      req = DirectCast(WebRequest.Create("http://www.example.com"), HttpWebRequest)
      Dim stream As Stream = req.GetResponseStream()

      Dim data As Byte() = New Byte(4095) {}
      Dim read As Integer
      While (InlineAssignHelper(read, data.Read(data, 0, data.Length))) > 0
            Process(data, read)
      End While
Finally
      If req IsNot Nothing Then
            req.Close()
      End If
End Try
Avatar of Larry Brister

ASKER

sl8rz,

What do I need for InlineAssignHelper and Process?

Do I need to import something?

I did find this

    Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, value As T) As T
        target = value
        Return value
    End Function

But Process?
It's in one of these:
Imports Microsoft.Build.Utilities
Imports Microsoft.Build.Framework
Yeah that code is pretty bad.

GetResponseStream will only exist on HttpWebResponse.   So I would change part of the code to read:

req = (HttpWebRequest)WebRequest.Create("http://www.example.com");
                WebResponse res = req.GetResponse();
                Stream stream = res.GetResponseStream();

Open in new window


Process is either going to be System.Diagnostics.Process namespace, or somebody has created a method called Process which takes two input parameters (data and read).

Without knowing what it is you're trying to achieve and what the original source contained it is a tough one.
angus_young_acdc

The data.read also doesn;t make sense.

It's not reading the Byte() ?
What are you trying to read?
I have an HttpWebRequest that posts some paramaters to a service

I'm supposed to receive an undending xml response stream back.
(My terminology may be off)
I get an initial response back

The connection stays open

And I get additional xml every few seconds...

My post is working
But after getting the first full chunk of XML Code...it exits the While Statement below.

So...
This code produces the first full XML Below...

Blah...blah...blah...
        ' Displaying the contents of the page to the console 
        Dim streamResponse As Stream = myHttpWebResponse.GetResponseStream()
        Dim streamRead As New StreamReader(streamResponse)
       

        Dim readBuff(2560) As [Char]

        Dim count As Integer = streamRead.Read(readBuff, 0, 2560)

        Debug.WriteLine(count)
        Debug.WriteLine(ControlChars.Cr + "The contents of the HTML page are :  ")
        If count = 0 Then
            GoTo Resend
        Else
            While count > 0
                Dim outputData As New [String](readBuff, 0, count)
                Debug.WriteLine(outputData)
                count = streamRead.Read(readBuff, 0, 2560)
            End While
        End If

Blah...blah...blah...

Open in new window



<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<org.m5.api.v1.Response xmlns:m5="http://www.m5net.com/org/m5/data/v2/cti" xmlns:csta="http://www.ecma-international.org/standards/ecma-323/csta/ed5">
    <ErrorCount>0</ErrorCount>
    <Id>1</Id>
    <Result xsi:type="org.m5.data.v2.cti.HostedConnectObject" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <csta:MonitorStartResponse>
            <csta:monitorCrossRefID>487</csta:monitorCrossRefID>
        </csta:MonitorStartResponse>
    </Result>
</org.m5.api.v1.Response>

And on exit in the debug wondow
I see this

The thread '<No Name>' (0x2280) has exited with code 0 (0x0)
My only thought regarding your premature exit from that while loop is to put in a sleep command just before it does another read (which fills your "count").  If it grabs the first available chunk and immediately looks again, the read will return nothing (even though there may be more a few milliseconds later).  So, if you use a "threading.thread.sleep(500)" command just before that read you'd get a half second of wait time for it to fill back up.  NOTE: 500 milliseconds may be too much - a half a second is a long time; 200 milliseconds may make more sense...but experiment and see what's best.
sl8rz
In my code above, where would I place that?
ASKER CERTIFIED SOLUTION
Avatar of David L. Hansen
David L. Hansen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Did that do it??
Thanks