本示例介绍如何使用阿里云Java SDK调用DescribeSecurityGroupsRequest接口查询已创建的安全组信息。

示例代码

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.ecs.model.v20140526.DescribeSecurityGroupsRequest;
import com.aliyuncs.ecs.model.v20140526.DescribeSecurityGroupsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;

public class ECSOperate {

	private static final String ACCESS_KEY = "";
	private static final String ACCESS_SECRET = "";
	private static IAcsClient client = null;

	static {
		//创建profile
		IClientProfile profile = DefaultProfile.getProfile("cn-shenzhen", ACCESS_KEY, ACCESS_SECRET);
		//创建client
		client = new DefaultAcsClient(profile);
	}

	public static String describeSecurityGroups(FormatType formatType) {
		//构造请求
		DescribeSecurityGroupsRequest request = new DescribeSecurityGroupsRequest();
		request.setAcceptFormat(formatType);
		try {
			//获取返回内容
			return client.doAction(request).getHttpContentString();
			// 获取返回对象
			//return client.getAcsResponse(request);
		} catch (ServerException e) {
			System.out.println("服务器异常:" + e);
			e.printStackTrace();
			return null;
		} catch (ClientException e) {
			System.out.println("参数配置错误:" + e);
			e.printStackTrace();
			return null;
		}
	}
	
	public static void main(String[] args) {
		System.out.println(describeSecurityGroups(FormatType.JSON));
	}
}