Solved

Invalid C#?

Posted on 2013-06-19
12
189 Views
Last Modified: 2013-06-20
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();
}
0
Comment
Question by:lrbrister
  • 6
  • 5
12 Comments
 
LVL 15

Expert Comment

by:David L. Hansen
ID: 39259837
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
0
 

Author Comment

by:lrbrister
ID: 39259859
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?
0
 
LVL 15

Expert Comment

by:David L. Hansen
ID: 39259924
It's in one of these:
Imports Microsoft.Build.Utilities
Imports Microsoft.Build.Framework
0
 
LVL 15

Expert Comment

by:angus_young_acdc
ID: 39259928
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.
0
 

Author Comment

by:lrbrister
ID: 39259982
angus_young_acdc

The data.read also doesn;t make sense.

It's not reading the Byte() ?
0
 
LVL 15

Expert Comment

by:David L. Hansen
ID: 39260116
What are you trying to read?
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:lrbrister
ID: 39260153
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)
0
 
LVL 15

Expert Comment

by:David L. Hansen
ID: 39260178
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.
0
 

Author Comment

by:lrbrister
ID: 39260215
sl8rz
In my code above, where would I place that?
0
 
LVL 15

Accepted Solution

by:
David L. Hansen earned 500 total points
ID: 39260294
right after the debug line in your while loop.
0
 
LVL 15

Expert Comment

by:David L. Hansen
ID: 39262681
Did that do it??
0
 

Author Closing Comment

by:lrbrister
ID: 39262935
Thanks
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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:…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

760 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

22 Experts available now in Live!

Get 1:1 Help Now