简体   繁体   English

将使用WinHttp.WinHttpRequest的简短vb6代码迁移到c#或vb.net

[英]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. 有人可以指导我如何将vb6代码转换为c#或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). 我试图使用System.Net.WebRequest()类但没有成功(我的所有东西都引发了另一个异常,因此我认为添加我的C#代码将无济于事)。

this code works fine (i tested it with the vba editor). 这段代码可以正常工作(我使用vba编辑器对其进行了测试)。

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. 澄清:我不希望做我的工作我只需要了解System.Net.WebRequest()类方法的GetHTTPResponse()函数中前11行的等效方法,就足够了,我将能够处理从那一点开始。

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

The best way to perform a web request in C# is using the HttpClient , as long as you support .NET 4.5+. 只要您支持.NET 4.5+,在C#中执行Web请求的最佳方法就是使用HttpClient 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. 是学习HttpClient的好资源。

Be sure to mark your GetHTTPResponse method with the async keyword. 确保使用async关键字标记您的GetHTTPResponse方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM