Solved

Invalid C#?

Posted on 2013-06-19
12
194 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

749 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