BillingClient
BillingClient is the client of the BILLING service of the trading system, providing a series of methods for developers to interact with the BILLING service, including bills. To use Java SDK to initiate a BILLING service http request, you need to initialize a BillingClient instance and modify the default configuration items of BosClientConfiguration as needed.
Use AK/SK to Create a BillingClient
When accessing Billing via AK/SK, You can refer to the following code to create a BillingClient:
public class Sample {
public static void main(String[] args) {
String ACCESS_KEY_ID = <your-access-key-id>; // Users’ Access Key ID
String SECRET_ACCESS_KEY = <your-secret-access-key>; // Users’ Secret Access Key
// Initialize a BillingClient
BillingClientConfiguration config = new BillingClientConfiguration();
config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID, SECRET_ACCESS_KEY));
config.setEndpoint("https://billing.baidubce.com");
BillingClient client = new BillingClient(config);
}
}
In the above code, ACCESS_KEY_ID
and SECRET_ACCESS_KEY
correspond to the "Access Key ID" and the "Access Key Secret" in the console, respectively. For more information on how to access it, please see the "Operation Guide Manage ACCESSKEY".
Configure HTTPS Protocol to Access Billing
Billing supports HTTPS transmission protocol. You can access the Billing service using HTTPS in Billing Java SDK in the following two ways:
-
Indicate https in endpoint
String endpoint = "http://billing.baidubce.com"; String ak = "ak"; String sk = "sk"; BillingClientConfiguration config = new BillingClientConfiguration(); config.setCredentials(new DefaultBceCredentials(ak, sk)); BillingClient client = new BillingClient(config);
-
Set the https protocol by calling the setProtocol method
String endpoint = "billing.baidubce.com"; // endpoint excluding protocol String ak = "ak"; String sk = "sk"; BillingClientConfiguration config = new BillingClientConfiguration(); config.setCredentials(new DefaultBceCredentials(ak, sk)); config.setEndpoint(ENDPOINT); config.setProtocol(Protocol.HTTPS); // if it is not specified, use http BillingClient client = new BillingClient(config);
Note: If protocol is indicated in endpoint, the entry in endpoint takes effect, and a separate call to setProtocol () does not work.
Configure BillingClient
If you need to configure parameters for some details of BillingClient, you can introduce the BillingClientConfiguration object when constructing BillingClient. BillingClientConfiguration is the configuration class for the BILLING service. It can configure parameters such as proxy and maximum number of connections for clients.
Use Agent
The following codes allow the client to use a proxy to access the BILLING service
String ACCESS_KEY_ID = <your-access-key-id>; // Users’ Access Key ID
String SECRET_ACCESS_KEY = <your-secret-access-key>; // Users’ Secret Access Key
String ENDPOINT = <domain-name>; // You specify your own name
// Create BillingClientConfiguration instance
BillingClientConfiguration config = new BillingClientConfiguration();
// Configure the agent as local 8080 interface
config.setProxyHost("127.0.0.1");
config.setProxyPort(8080);
// Create Billing client
config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID,SECRET_ACCESS_KEY));
config.setEndpoint(ENDPOINT);
BillingClientclient = new BillingClient (config);
Using the code segment above, all operations of client are executed as an agent via the 8080 interface of 127.0.0.1 address. For the agent with user authentication, the following code segment can be used to configure username and password.
// Create BillingClientConfiguration instance
BillingClientConfiguration config = new BillingClientConfiguration ();
// Configure the agent as local 8080 interface
config.setProxyHost("127.0.0.1");
config.setProxyPort(8080);
// Set username and password
config.setProxyUsername(<username>); //Username
config.setProxyPassword(<password>); // Password
Users can set basic network parameters with BillingClientConfiguration:
BillingClientConfiguration config = new BillingClientConfiguration();
// Set maximum connections of HTTP as 10.
config.setMaxConnections(10);
// Set TCP connection timeout as 5,000ms.
config.setConnectionTimeout(5000);
// Set the time of Socket transmission data timeout as 2,000ms.
config.setSocketTimeout(2000);
Parameter description
All parameters that can be designated through BillingClientConfiguration are listed in the following table:
Parameter | Description |
---|---|
UserAgent | User agent, referring to the HTTP User-Agent header |
Protocol | Connection protocol type |
ProxyDomain | Windows domain name for access to NTLM verified proxy server |
ProxyHost | Proxy server host address |
ProxyPort | Proxy server interface |
ProxyUsername | User name for proxy server authentication |
ProxyPassword | Password for proxy server authentication |
ProxyPreemptiveAuthenticationEnabled | Whether to set up user agent authentication |
ProxyWorkstation | Windows workstation name of the NTLM proxy server |
LocalAddress | Local address |
ConnectionTimeoutInMillis | Timeout for establishing a connection (unit: millisecond) |
SocketTimeoutInMillis | Timeout for transmitting data over open connections (unit: millisecond) |
MaxConnections | Maximum number of HTTP connections allowed to open |
RetryPolicy | Connection retry policy |
SocketBufferSizeInBytes | Socket buffer size |
StreamBufferSize | Stream file buffer size |