Jonathan Applebaum
Jonathan Applebaum

Reputation: 5986

Migrating short vb6 code that uses WinHttp.WinHttpRequest to c# or vb.net

can someone please guide me how to convert that vb6 code to c# or visual basic.net. i have tried to to use the  System.Net.WebRequest() class but with no success (everything i treys throws another exception so i think that adding my c# code will not be helpful).

this code works fine (i tested it with the vba editor).

clarification: I DONT EXPECT SOME TO DO MY JOB i just need to understand the equivilent methods of the first 11 rows in GetHTTPResponse() function to System.Net.WebRequest() class medhods , it will be enough and i will be able to handle it alone from that point.

thank you very much for your time and consideration.

Function ReadToken(s)
    Dim X()
    ReDim X(0)
    s = Replace(s, Chr(34), "")
    bRes = False
    Set RegExp = CreateObject("VBScript.RegExp")
    RegExp.IgnoreCase = True
    RegExp.Pattern = "token\:(.+?),type\:(.+?),expires_in\:(.+?)"

    bRes = RegExp.test(s)
    If bRes Then
        Set oMatches = RegExp.Execute(s)
        ReDim X(2)
        X(0) = oMatches(0).subMatches(0)
        X(1) = oMatches(0).subMatches(1)
        X(2) = oMatches(0).subMatches(2)
    End If
    ReadToken = X

End Function

Function GetHTTPResponse(ByVal Id As Long) As String
' On Error Resume Next
    Const username = "username"
    Const password = "password"
    sURL = "https://api.exoclick.com/v1/login"
    Set oXMLHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
    With oXMLHTTP
        .Open "Post", sURL, False
        .setRequestHeader "Content-Type", "application/json"
        params = "{""username"":" & Chr(34) & username & Chr(34) & ", ""password"":" & _
                Chr(34) & password & Chr(34) & "}"
        .send params
        If .Status = 200 Then
            Response = .ResponseText
            Token = ReadToken(Response)
            Authorization = "{""type"":" & Chr(34) & Token(1) & _
                            Chr(34) & ",""token"":" & Chr(34) & Token(0) & Chr(34) & "}"
            .abort
            sURL = "https://api.exoclick.com/v1/statistics/advertiser/date"
            params = "{""campaignid"":" & Id & "}"
            .Open "GET", sURL, False
            .setRequestHeader "Content-Type", "application/json"
            .setRequestHeader "Authorization", Authorization
            .send params
            GetHTTPResponse = .ResponseText
        End If
    End With
    Set oXMLHTTP = Nothing
End Function

Upvotes: 0

Views: 6277

Answers (1)

CC Inc
CC Inc

Reputation: 5928

The best way to perform a web request in C# is using the HttpClient , as long as you support .NET 4.5+. Here's a simple example:

using (var client = new HttpClient())
{
    var values = new Dictionary<string, string>
    {
       { "username", username },
       { "password", password }
    };

    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    var content = new FormUrlEncodedContent(values);
    var response = await client.PostAsync(sURL, content);
    if (response.IsSuccessStatusCode)
    {    
        var response = await response.Content.ReadAsString();
        // then repeat for the next request
    }
}

Here is a good resource for learning about HttpClient.

Be sure to mark your GetHTTPResponse method with the async keyword.

Upvotes: 3

Related Questions