Use Java to call Restapi to set up a classic Linux virtual machine to start and stop an instance, restapilinux

Source: Internet
Author: User

Use Java to call Restapi to set up a classic Linux virtual machine to start and stop an instance, restapilinux
Symptom description

You can use the Rest API to set the start and stop of a classic Linux Virtual Machine instance. When calling this API, you must obtain the Token through Azure Active Directory (AAD). However, because the Token obtained by the Application of AAD in Azure in China cannot operate the classic API, therefore, you need to obtain the Token through the Client ID and the Administrator's username and password.

Prerequisites

Create a Linux classic VM.

Sample Code

Java

import java.io.DataOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.net.URI;import java.net.URISyntaxException;import java.net.URL;import java.security.KeyManagementException;import java.security.*;import java.security.KeyStoreException;import java.security.NoSuchAlgorithmException;import java.security.UnrecoverableKeyException;import java.security.cert.X509Certificate;import java.util.HashMap;import java.util.Map;import java.util.Scanner;import java.util.concurrent.ExecutionException;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Future;import javax.net.ssl.*;import javax.net.ssl.KeyManagerFactory;import javax.net.ssl.SSLSocketFactory;import javax.net.ssl.TrustManager;import org.codehaus.jackson.map.ObjectMapper;//get Access token for Rest APIpublic void GetToken() {     ExecutorService service = Executors.newFixedThreadPool(1);    AuthenticationContext ac = new AuthenticationContext("https://login.chinacloudapi.cn/tenantID", true, service);     Future future = ac.acquireToken("https://management.core.chinacloudapi.cn/",  "1950a258-227b-4e31-a9cf-717495945fc2", "username", "password", null);    AuthenticationResult result = future.get();    String token = result.getAccessToken();    rest(token);}public static void rest(String accessToken) throws IOException{    URL url = new URL(String.format("https://management.core.chinacloudapi.cn/subID/services/hostedservices/{hostedservices}/deployments/{deployments}/roleinstances/{roleinstancesName}/Operations"));    HttpsURLConnection conn = (HttpsURLConnection)url.openConnection();    conn.setRequestProperty("x-ms-version", "2013-06-01");    conn.setRequestProperty("Authorization", "Bearer " + accessToken);    conn.setRequestProperty("Content-Type", "application/xml");    //StartRole    String roleInstance = new String("
 
  \n" + "
  
   StartRoleOperation
  \n" +                               "
 ");    //ShutdownRole                 String roleins = new String( "
 
  " +"
  
   ShutdownRoleOperation
  " +                                           "
  
   StoppedDeallocated
  " +                                               "
 ");    byte[] data = roleInstance.getBytes();    conn.setDoOutput(true);    conn.setRequestMethod("POST");    if (data != null)    {        DataOutputStream requestStream = new DataOutputStream(conn.getOutputStream());        requestStream.write(data);        requestStream.flush();        requestStream.close();    }    String mess =  conn.getResponseMessage();    int code = conn.getResponseCode();     InputStream input = conn.getErrorStream();    if (input == null)    input = conn.getInputStream();    String response = null;    try (Scanner scanner = new Scanner(input)) {        scanner.useDelimiter("\\Z");        response = scanner.next();        scanner.close();        input.close();    }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.