从 .net 应用程序中使用 APIM api

consuming APIM api from .net application

我的跨团队成员在 APIM 中创建了一个 API,我必须将其用于现有的 .net 应用程序。他分享了邮递员collection和环境。我已经导入它并使用 Postman 成功取回了结果。

我必须从现有的 .net 应用程序调用此 API,但我不知道该怎么做。任何 pointers/inputs/help 将不胜感激。

以下是我实施的解决方案。我希望它能帮助别人。 -- 我使用 HttpClient 进行调用并获取令牌。

Dim client = New HttpClient()

client.BaseAddress = "https://dev-azure.com" '希望响应为 JSON。 client.DefaultRequestHeaders.Accept.Clear() client.DefaultRequestHeaders.Accept.Add(新的 MediaTypeWithQualityHeaderValue("application/json"))

        'Build up the data to POST.
        Dim postData As List(Of KeyValuePair(Of String, String)) = New List(Of KeyValuePair(Of String, String))()
        postData.Add(New KeyValuePair(Of String, String)("grant_type", "yourValue"))
        postData.Add(New KeyValuePair(Of String, String)("client_id", "yourvalue"))
        postData.Add(New KeyValuePair(Of String, String)("client_secret", "yourvlaue"))
        postData.Add(New KeyValuePair(Of String, String)("resource", yourvlaue))
        Dim content = New FormUrlEncodedContent(postData)

        'Post to the Server And parse the response.
        Dim responseContent = client.PostAsync("token", content).Result

        If (responseContent.StatusCode = HttpStatusCode.OK) Then
            Dim jsonString = responseContent.Content.ReadAsStringAsync().Result
            Dim responseData = JsonConvert.DeserializeObject(jsonString)
            accessToken = responseData("access_token")
        End If
    End Using