Results 1 to 5 of 5

Thread: Python to VB

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    1,958

    Python to VB

    I'm trying to convert a python script to VB.net. The python line I'm working on is as follows:

    Code:
    res = ses.get(url, headers=myHeader, timeout=15, verify=False)
    I think I need to use HttpClient, but I'm not sure how to add the header, timeout & verify parameters.
    I haven't worked the HttpClient very much, so looking for a little help. Thanks...

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,673

    Re: Python to VB

    I'm not sure about the verify=False, but this is how you'd submit a GET request specifying the headers:
    Code:
    Dim requestUri = "-change me-"
    
    Try
        Dim client = New HttpClient()
        client.DefaultRequestHeaders.Add("-key-", "-value-")
    
        Dim response = client.GetAsync(requestUri).Result
        If response.IsSuccessStatusCode Then
            Dim content As String = response.Content.ReadAsStringAsync().Result
    
            Console.WriteLine(content)
        Else
            Console.WriteLine("Request failed with status code {0}", response.StatusCode)
        End If
    Catch ex As Exception
        Console.WriteLine("Error: {0}", ex.Message)
    End Try
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    1,958

    Re: Python to VB

    Thanks dday ... I'll give that a try.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    1,958

    Re: Python to VB

    dday ... can you explain the key & value? I'm not sure what I need to use there. Here is a sample of the header I need to use:

    Code:
    {'User-Agent': 'Mozilla/5.0 (QtEmbedded; U; Linux; C) AppleWebKit/533.3 (KHTML, like Gecko) Mobile Safari/533.3', 'Referer': 'http://myurl.com/c/', 'Accept': 'application/json,application/javascript,text/javascript,text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Encoding': 'gzip, deflate', 'Connection': 'Keep-Alive', 'X-User-Agent': 'Model: ABC123; Link: Ethernet'}

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,673

    Re: Python to VB

    What you provided is a JSON object (documentation) and the way that they are defined as key/value pairs, wrapped in quotes, with the key/value pair being separated by a colon and multiple key/value pairs by commas.

    This is what your JSON would look like formatted:
    Code:
    {
        "User-Agent": "Mozilla/5.0 (QtEmbedded; U; Linux; C) AppleWebKit/533.3 (KHTML, like Gecko) Mobile Safari/533.3",
        "Referer": "http://myurl.com/c/",
        "Accept": "application/json,application/javascript,text/javascript,text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
        "Accept-Encoding": "gzip, deflate",
        "Connection": "Keep-Alive",
        "X-User-Agent": "Model: ABC123; Link: Ethernet"
    }
    In this example, there are 6 total key/value pairs with the keys being:
    1. User-Agent (documentation)
    2. Referer (documentation)
    3. Accept (documentation)
    4. Accept-Encoding (documentation)
    5. Connection (documentation)
    6. X-User-Agent (this is not a defined header, which is perfectly normal)


    In this case, you can probably drop 1, 2, 5, and 6. So in your case, the VB.NET code would look something like this:
    Code:
    client.DefaultRequestHeaders.Add("Accept", "application/json,application/javascript,text/javascript,text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
    client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate")
    If you want more information, MDN is a good reference on HTTP requests. You can find their documentation here: https://developer.mozilla.org/en-US/docs/Web/HTTP
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

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



Click Here to Expand Forum to Full Width