Solved

Invalid C#?

Posted on 2013-06-19
12
195 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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
 

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

751 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