我试图通过该方法获取您的管道命令的健康值health()
班级的ApplicationGatewayBackendHealthServer https://learn.microsoft.com/en-us/java/api/com.microsoft.azure.management.network._application_gateway_backend_health_serverAzure Java SDK,但失败了,因为它似乎没有实现,我无法获得ApplicationGatewayBackendHealthServer
通过根类的实现路径的对象Azure
now.
所以我尝试搜索相关的REST API来获取命令的响应azure network application-gateway backend-health show <resource-group-name> <applicationgateway-name>
在 Azure REST API 上docs https://learn.microsoft.com/en-us/rest/api/applicationgateway/get-application-gateway#request-body,但也失败了,不存在。
我尝试研究源代码AzureCLI
和其他SDK,最后我从详细日志中得到了你想要的REST APIazure.details.log
of AzureCLI
.
满足您需求的 REST API 如下所示,为第一步。
https://management.azure.com/subscriptions/<subscriptionId>/resourceGroups/<resource-group-name>/providers/Microsoft.Network/applicationGateways/<applicationGateway-name>/backendhealth?api-version=<api-version>
正在做的POST
对上述带有标头的 REST API 的请求Authorization: Bearer <accessToken>
,可以从响应头获取下一个动态REST APIlocation
就像下面通过GET
带标头的请求Authorization: Bearer <accessToken>
.
https://management.azure.com/subscriptions/<subscriptionId>/providers/Microsoft.Network/locations/<region>/operationResults/<objectId, such as f7bfd1fd-e3ea-42f7-9711-44f3229ff877>?api-version=<api-version>
这是我的示例代码。
String AUTHORITY = "https://login.windows.net/<tenantId>";
String clientId = "<client-id on management portal(old), or application-id on Azure new portal>";
String clientSecret = "<client-secret-key>";
String subscriptionId = "<subscriptionId>";
String resourceGroupName = "<resource-group-name>";
String appGatewayName = "<applicationgateway-name>";
String apiVersion = "2016-09-01";
// Getting access token
AuthenticationContext context = null;
AuthenticationResult result = null;
ExecutorService service = null;
service = Executors.newFixedThreadPool(1);
context = new AuthenticationContext(AUTHORITY, false, service);
ClientCredential credential = new ClientCredential(clientId, clientSecret);
Future<AuthenticationResult> future = context.acquireToken("https://management.azure.com/", credential, null);
result = future.get();
String accessToken = result.getAccessToken();
System.out.println(accessToken);
使用第一步 REST API:
// Get the response header `location` from 1st step REST API
OkHttpClient client = new OkHttpClient();
String url = String.format("https://management.azure.com/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/applicationGateways/%s/backendhealth?api-version=%s", subscriptionId, resourceGroupName, appGatewayName, apiVersion);
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
RequestBody body = RequestBody.create(JSON, "");
Request request = new Request.Builder().url(url).header("Authorization", "Bearer "+accessToken).post(body).build();
Response response = client.newCall(request).execute();
String location = response.header("Location");
System.out.println(location);
使用第二步动态 REST API:
// Get the response content as the same as the azure-cli command `azure network applicationgateway backend-health show`
Request request2 = new Request.Builder().url(location).header("Authorization", "Bearer " + accessToken).build();
// Notice for the below code, see under the code
Response response2 = client.newCall(request2).execute();
System.out.println(response2.body().string());
Notice:
我能够通过以下方式获取内容POSTMAN
,但是我得到了回复内容null
对于第二步,使用OKHttp
/Apache HttpClient
/HttpURLConnection
在爪哇。我不知道发生了什么以及为什么,即使在 Golang/Jquery 中实现的代码也可以正常工作。看来是Java中HTTP协议栈的实现造成的。
同时,如果您收到上述REST API权限相关的错误信息,请参考https://github.com/JamborYao/ArmManagement https://github.com/JamborYao/ArmManagement来解决它。
希望对您有帮助。如果您能解决第二步问题,请与我们分享解决方案,我将继续尝试解决它。