VS 2017 Trying to download json and read the array data.-VBForums
Results 1 to 14 of 14

Thread: Trying to download json and read the array data.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2017
    Posts
    24

    Trying to download json and read the array data.

    http://73.209.129.87:42001/getstat that's the json i get.

    I tried WebClient.DownloadString("http://73.209.129.87:42001/getstat") that gives me an error. Only way i got it to work is with WebBrowser1 because of the HTML on that page and i convert it to a string and then had some problems reading the string for an array but that's dirty.

    Any help would be appreciated.
    Last edited by infowre; Oct 2nd, 2017 at 10:09 AM.

  2. #2
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    3,805

    Re: Trying to download json and read the array data.

    When I click on it, all I get is the text "Not found". That both indicates an error and is not valid JSON.

    If you could post what the JSON looks like and what your code to parse it looks like, we could help you more. In particular: DownloadString() is only going to get you halfway there. JSON represents object data, so you have to parse it to deal with it as data. DownloadString() is just the first step. If you can show us the JSON, we can definitely help. But we can't help you blind.
    Nothing I post is production-ready. It is provided as-is, use it at your own risk.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2017
    Posts
    24

    Re: Trying to download json and read the array data.

    oops, there shouldn't be a slash at the end. I tried DownloadString. Maybe i am doing something wrong.

  4. #4
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    3,805

    Re: Trying to download json and read the array data.

    It helps if you post the error. It's a WebException with message "The server committed a protocol violation. Section=ResponseStatusLine."

    Apparently this happens when servers do a variety of things that break protocol. I tried my best to look at what your server was returning and figure out what was wrong. There's only one header in the response, and the only thing that MIGHT be wrong is it seems like the server is returning 2,000 characters of data but claiming Content-Length of 2001.

    Browsers and several other clients handle it because they expect abuse. WebClient seems to be fairly picky unless you muck with its settings, which is smart and the safe thing to do in an HTTP library.

    Anyway, there seems to be a solution, though whatever server is serving that data should be investigated.
    Nothing I post is production-ready. It is provided as-is, use it at your own risk.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    30,820

    Re: Trying to download json and read the array data.

    I'm no longer clear on what the question is. I see that the original post was edited, and post #3 suggests why, but when I click on the link I get what certainly looks like valid JSON. Has the error gone away? Does this mean the question is now resolved, or now a question about parsing this particular JSON?
    My usual boring signature: Nothing

  6. #6
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    3,805

    Re: Trying to download json and read the array data.

    The score as I see it:

    There's probably other code that does the relevant parsing, but OP isn't getting there because trying to download the data throws an exception. The exception is being thrown because the server is not conforming 100% to the HTTP specification, but I can't figure out exactly how it deviates. Whatever it is, it ticks off WebClient.

    So no answers RE: the JSON has been given, because you can't parse what you can't download, and I'd have to see the parsing code to comment. I think the protocol violation exception is probably the problem that led to the thread.
    Nothing I post is production-ready. It is provided as-is, use it at your own risk.

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    31,103

    Re: Trying to download json and read the array data.

    Took a minute with Fiddler... it's sending an incorrect mime type... it's reporting it as html rather than json content. A browser just deals with it because html is jsut text anyways, so when it doesn't find any mark up, it just sends it as raw text and displays it because browsers are designed to degrade nicely.... WEbclient on the other hand is being a bit more picky and telling you, "hey this data is claiming to be html, but it doesn't look like any html I've ever seen, it could be suspect, so I'ma gonna bail." And then it does. My guess, get the server to send back that the data is text/json in the headers, that will fix the problem with the DownloadString, then we can tackle the parsing.

    Although that does bring up the question - is the server that's the source of this data something that's in your control?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Feb 2017
    Posts
    24

    Re: Trying to download json and read the array data.

    Nope don't control the server ;(.
    Any way to force read it as json data ?

  9. #9
    Fanatic Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    519

    Re: Trying to download json and read the array data.

    You could try using the headers property https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx and add in an Accepts header with a value of "application/json" that might convince the server to set the header accordingly.

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Feb 2017
    Posts
    24

    Re: Trying to download json and read the array data.

    Code:
            Dim client As New WebClient()
            client.Headers("Content-Type") = "application/json"
            client.Headers("Accept") = "application/json"
            Dim arr As Byte() = client.DownloadData("http://73.209.129.87:42001/getstat")
    Still get the System.Net.WebException: 'The server committed a protocol violation. Section=ResponseStatusLine

    what if i do it in php on my own server that will convert it on the fly to application/json?
    Last edited by infowre; Oct 2nd, 2017 at 02:56 PM.

  11. #11
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    3,805

    Re: Trying to download json and read the array data.

    Did you try the configuration change in the link I gave you in #4?

    If all else fails, you can always try using TCP instead of HTTP. That'll get you all the raw data and it isn't hard to throw away the headers and deal with the raw data. I've had to roll up my sleeves and do it before. I'm about to have to run some errands but if no one else has made an attempt by then I can whip up an example tomorrow morning.
    Nothing I post is production-ready. It is provided as-is, use it at your own risk.

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Feb 2017
    Posts
    24

    Re: Trying to download json and read the array data.

    Yeah tried #4, thank you. Ill keep trying.

    {EDIT}

    I got it to work this way.... Using PHP.

    PHP Code:
    <?
    header
    ("Content-Type: application/json; charset=UTF-8");
    echo 
    $html file_get_contents("http://".$_GET["ip"]."/getstat");
    />
    Code:
            Dim wc As New WebClient
    
            Dim value As String = wc.DownloadString("http://elvis87.com/test.php?ip=73.209.129.87:42001")
    
            Dim result = JsonConvert.DeserializeObject(value)
    
            MsgBox(result("result")(6)("name"))
    
            Dim token = JToken.Parse(value)
            Dim count = token.SelectTokens("result[*].gpuid").Count
    
    
            For i As Integer = 0 To count - 1
                MsgBox(i) 'How many cards there are.
            Next
    Last edited by infowre; Oct 2nd, 2017 at 04:39 PM.

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Feb 2017
    Posts
    24

    Re: Trying to download json and read the array data.

    i still would like a solution that doesn't involve php tho.

  14. #14
    Fanatic Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    519

    Re: Trying to download json and read the array data.

    I haven't had time to try this and I am dashing off to catch a train but https://msdn.microsoft.com/en-us/lib...v=vs.118).aspx might give you more control compared to the WebClient class, worth a try and the code isn't really any more difficult.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.