Solved

Invalid C#?

Posted on 2013-06-19
12
192 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to prevent website from publishing with errors 9 46
Cant save 3D 4 19
No Data for DropDown List 2 24
Function after success of Jquery/AJAX action 9 11
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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 shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

770 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