Configure a proxy

Google Cloud client libraries use HTTPS and gRPC in underlying communication with the services. In both protocols, you can configure a proxy using https.proxyHost and, optionally, https.proxyPort properties.

Configure a proxy with HTTP

For HTTP clients, you can configure a basic proxy by using http.proxyHost and related system properties, as documented by Java Networking and Proxies.

For a custom proxy (for example, an authenticated proxy), provide a custom HttpTransportFactory to GoogleCredentials:

importcom.google.api.client.http.HttpTransport ;
importcom.google.api.client.http.apache.v2.ApacheHttpTransport ;
importcom.google.auth.http.HttpTransportFactory ;
importcom.google.auth.oauth2.GoogleCredentials ;
importorg.apache.http.HttpHost;
importorg.apache.http.auth.AuthScope;
importorg.apache.http.auth.UsernamePasswordCredentials;
importorg.apache.http.client.CredentialsProvider;
importorg.apache.http.client.HttpClient;
importorg.apache.http.conn.routing.HttpRoutePlanner;
importorg.apache.http.impl.client.BasicCredentialsProvider;
importorg.apache.http.impl.client.ProxyAuthenticationStrategy;
importorg.apache.http.impl.conn.DefaultProxyRoutePlanner;
importjava.io.IOException;
publicclass ProxyExample{
publicGoogleCredentials getCredentials()throwsIOException{
HttpTransportFactory httpTransportFactory=getHttpTransportFactory(
"some-host",8080,"some-username","some-password"
);
returnGoogleCredentials .getApplicationDefault (httpTransportFactory);
}
publicHttpTransportFactory getHttpTransportFactory(StringproxyHost,intproxyPort,StringproxyUsername,StringproxyPassword){
HttpHostproxyHostDetails=newHttpHost(proxyHost,proxyPort);
HttpRoutePlannerhttpRoutePlanner=newDefaultProxyRoutePlanner(proxyHostDetails);
CredentialsProvidercredentialsProvider=newBasicCredentialsProvider();
credentialsProvider.setCredentials(
newAuthScope(proxyHostDetails.getHostName(),proxyHostDetails.getPort ()),
newUsernamePasswordCredentials(proxyUsername,proxyPassword)
);
HttpClienthttpClient=ApacheHttpTransport .newDefaultHttpClientBuilder()
.setRoutePlanner(httpRoutePlanner)
.setProxyAuthenticationStrategy(ProxyAuthenticationStrategy.INSTANCE)
.setDefaultCredentialsProvider(credentialsProvider)
.build();
finalHttpTransport httpTransport=newApacheHttpTransport (httpClient);
returnnewHttpTransportFactory (){
@Override
publicHttpTransport create(){
returnhttpTransport;
}
};
}
}

The preceding example requires com.google.http-client:google-http-client-apache-v2.

Configure a proxy with a gRPC custom proxy configuration

For a custom proxy with gRPC, supply a ProxyDetector to the ManagedChannelBuilder:

Replace PROXY_USERNAME, PROXY_PASSWORD, PROXY_HOST, and PROXY_PORT with the credentials and address of your proxy.

importcom.google.api.core.ApiFunction ;
importcom.google.api.gax.rpc.TransportChannelProvider ;
importcom.google.cloud.tasks.v2.CloudTasksClient ;
importcom.google.cloud.tasks.v2.CloudTasksSettings ;
importcom.google.cloud.tasks.v2.stub.CloudTasksStubSettings;
importio.grpc.HttpConnectProxiedSocketAddress;
importio.grpc.ManagedChannelBuilder;
importio.grpc.ProxiedSocketAddress;
importio.grpc.ProxyDetector;
importjavax.annotation.Nullable;
importjava.io.IOException;
importjava.net.InetSocketAddress;
importjava.net.SocketAddress;
publicCloudTasksClient getService()throwsIOException{
TransportChannelProvider transportChannelProvider=
CloudTasksStubSettings.defaultGrpcTransportProviderBuilder ()
.setChannelConfigurator(
newApiFunction<ManagedChannelBuilder,ManagedChannelBuilder>(){
@Override
publicManagedChannelBuilderapply(ManagedChannelBuildermanagedChannelBuilder){
returnmanagedChannelBuilder.proxyDetector(
newProxyDetector(){
@Nullable
@Override
publicProxiedSocketAddressproxyFor(SocketAddresssocketAddress)
throwsIOException{
returnHttpConnectProxiedSocketAddress.newBuilder()
.setUsername(PROXY_USERNAME)
.setPassword(PROXY_PASSWORD)
.setProxyAddress(newInetSocketAddress(PROXY_HOST,PROXY_PORT))
.setTargetAddress((InetSocketAddress)socketAddress)
.build();
}
});
}
})
.build();
CloudTasksSettings cloudTasksSettings=
CloudTasksSettings .newBuilder()
.setTransportChannelProvider(transportChannelProvider)
.build();
returnCloudTasksClient .create(cloudTasksSettings);
}

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025年10月30日 UTC.