本文整理汇总了Java中java.net.Proxy.Type.HTTP属性的典型用法代码示例。如果您正苦于以下问题:Java Type.HTTP属性的具体用法?Java Type.HTTP怎么用?Java Type.HTTP使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类java.net.Proxy.Type
的用法示例。
在下文中一共展示了Type.HTTP属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createProxy
@VisibleForTesting
public Proxy createProxy(URI uri) {
Preconditions.checkNotNull(uri, "uri is null");
Preconditions.checkArgument(!"http".equals(uri.getScheme()), "http is not a supported schema");
IProxyService proxyServiceCopy = proxyService;
if (proxyServiceCopy == null) {
return Proxy.NO_PROXY;
}
IProxyData[] proxyDataForUri = proxyServiceCopy.select(uri);
for (final IProxyData iProxyData : proxyDataForUri) {
switch (iProxyData.getType()) {
case IProxyData.HTTPS_PROXY_TYPE:
return new Proxy(Type.HTTP, new InetSocketAddress(iProxyData.getHost(),
iProxyData.getPort()));
case IProxyData.SOCKS_PROXY_TYPE:
return new Proxy(Type.SOCKS, new InetSocketAddress(iProxyData.getHost(),
iProxyData.getPort()));
default:
logger.warning("Unsupported proxy type: " + iProxyData.getType());
break;
}
}
return Proxy.NO_PROXY;
}
示例2: convert
protected void convert(String pacResult, List<Proxy> result) {
Matcher m = PAC_RESULT_PATTERN.matcher(pacResult);
while (m.find()) {
String scriptProxyType = m.group(1);
if ("DIRECT".equals(scriptProxyType)) {
result.add(Proxy.NO_PROXY);
} else {
Type proxyType;
if ("PROXY".equals(scriptProxyType)) {
proxyType = Type.HTTP;
} else if ("SOCKS".equals(scriptProxyType)) {
proxyType = Type.SOCKS;
} else {
// Should never happen, already filtered by Pattern.
throw new RuntimeException("Unrecognized proxy type.");
}
result.add(new Proxy(proxyType, new InetSocketAddress(m
.group(2), Integer.parseInt(m.group(3)))));
}
}
}
示例3: getProxyFromProxyData
private Proxy getProxyFromProxyData(IProxyData proxyData) throws UnknownHostException {
Type proxyType;
if (IProxyData.HTTP_PROXY_TYPE.equals(proxyData.getType())) {
proxyType = Type.HTTP;
} else if (IProxyData.SOCKS_PROXY_TYPE.equals(proxyData.getType())) {
proxyType = Type.SOCKS;
} else if (IProxyData.HTTPS_PROXY_TYPE.equals(proxyData.getType())) {
proxyType = Type.HTTP;
} else {
throw new IllegalArgumentException("Invalid proxy type " + proxyData.getType());
}
InetSocketAddress sockAddr = new InetSocketAddress(InetAddress.getByName(proxyData.getHost()), proxyData.getPort());
Proxy proxy = new Proxy(proxyType, sockAddr);
if (!Strings.isNullOrEmpty(proxyData.getUserId())) {
Authenticator.setDefault(new ProxyAuthenticator(proxyData.getUserId(), proxyData.getPassword()));
}
return proxy;
}
示例4: initHttpProxy
/**
* 初始化HTTP代理服务器
*/
private void initHttpProxy() {
// 初始化代理服务器
if (httpProxyHost != null && httpProxyPort != null) {
SocketAddress sa = new InetSocketAddress(httpProxyHost, httpProxyPort);
proxy = new Proxy(Type.HTTP, sa);
// 初始化代理服务器的用户验证
if (httpProxyUsername != null && httpProxyPassword != null) {
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(httpProxyUsername, httpProxyPassword.toCharArray());
}
});
}
}
}
示例5: restTemplate
@Bean
public RestTemplate restTemplate() {
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
Proxy proxy= new Proxy(Type.HTTP, new InetSocketAddress("vip-px.main.aviva.eu.corp", 8080));
requestFactory.setProxy(proxy);
return new RestTemplate(requestFactory);
// return new RestTemplate();
}
示例6: openConnection
private HttpURLConnection openConnection ( final URL url ) throws IOException
{
final String host = this.properties.getProperty ( "local.proxy.host" );
final String port = this.properties.getProperty ( "local.proxy.port" );
if ( host != null && port != null && !host.isEmpty () && !port.isEmpty () )
{
final Proxy proxy = new Proxy ( Type.HTTP, new InetSocketAddress ( host, Integer.parseInt ( port ) ) );
return (HttpURLConnection)url.openConnection ( proxy );
}
else
{
return (HttpURLConnection)url.openConnection ();
}
}
示例7: toType
Type toType(){
switch (this){
case SOCKS: return Type.SOCKS;
case HTTP: return Type.HTTP;
}
return Type.DIRECT;
}
示例8: setHttpProxy
public void setHttpProxy(String host, int port) {
if (host != null && !host.isEmpty()) {
// Debug
logger.debug("Using HTTP proxy for REST connection: " + host + ":" + port);
// Set proxy
SimpleClientHttpRequestFactory requestFactory = (SimpleClientHttpRequestFactory) getRequestFactory();
Proxy proxy = new Proxy(Type.HTTP, new InetSocketAddress(host, port));
requestFactory.setProxy(proxy);
}
}
示例9: proxiedRequestToItsOwnUrlWithoutIllEffect
@Test
public void proxiedRequestToItsOwnUrlWithoutIllEffect() throws Exception {
Request request = new Request.Builder().url("http://localhost:9092/").build();
Proxy proxy = new Proxy(Type.HTTP, new InetSocketAddress("localhost", 9092));
Response response = client.newBuilder().proxy(proxy).build().newCall(request).execute();
assertEquals(HttpURLConnection.HTTP_NOT_FOUND, response.code());
// HTTP 1.1 persistent connection needs content-length
assertEquals(Protocol.HTTP_1_1, response.protocol());
assertEquals("24", response.header("content-length"));
assertEquals("text/plain; charset=UTF-8", response.header("content-type"));
assertEquals("Failure: 404 Not Found", response.body().source().readUtf8Line());
// since proxied URI is http://localhost:9092/ instead of /
// TODO should respond cached or upstream content
}
示例10: securedProxiedRequestToItsOwnUrlWithoutIllEffect
@Test
public void securedProxiedRequestToItsOwnUrlWithoutIllEffect() throws Exception {
Request request = new Request.Builder().url("https://localhost:9092/").build();
Proxy proxy = new Proxy(Type.HTTP, new InetSocketAddress("localhost", 9092));
Response response = client.newBuilder().proxy(proxy).build().newCall(request).execute();
assertEquals(HttpURLConnection.HTTP_OK, response.code());
// HTTP 1.1 persistent connection needs content-length
assertEquals(Protocol.HTTP_1_1, response.protocol());
assertEquals("25", response.header("content-length"));
assertEquals("text/plain; charset=UTF-8", response.header("content-type"));
assertEquals("Response status: 200 OK", response.body().source().readUtf8Line());
// since proxied URI is /, TODO needs host and port from CONNECT
// TODO should respond cached or upstream content
}
示例11: testConstructorWithProxy
@Test
public void testConstructorWithProxy() throws MalformedURLException, IOException {
Proxy proxy =
new Proxy(Type.HTTP,
new InetSocketAddress(proxyServer.getHostname(), proxyServer.getPort()));
HttpURLConnection connection =
new TimeoutAwareConnectionFactory(proxy).openConnection(new URL(NONEXISTENTDOMAIN));
connectReadAndDisconnect(connection);
}
开发者ID:GoogleCloudPlatform,项目名称:google-cloud-eclipse,代码行数:9,代码来源:TimeoutAwareConnectionFactoryTest.java
示例12: testConstructorWithProxyAndTimeouts
@Test
public void testConstructorWithProxyAndTimeouts() throws MalformedURLException, IOException {
Proxy proxy =
new Proxy(Type.HTTP,
new InetSocketAddress(proxyServer.getHostname(), proxyServer.getPort()));
HttpURLConnection connection =
new TimeoutAwareConnectionFactory(proxy, 1764, 3528)
.openConnection(new URL(NONEXISTENTDOMAIN));
connectReadAndDisconnect(connection);
assertThat(connection.getConnectTimeout(), is(1764));
assertThat(connection.getReadTimeout(), is(3528));
}
开发者ID:GoogleCloudPlatform,项目名称:google-cloud-eclipse,代码行数:12,代码来源:TimeoutAwareConnectionFactoryTest.java
示例13: getConnection
private HttpURLConnection getConnection(String url) throws IOException {
HttpURLConnection con = null;
if (proxyHost != null && !proxyHost.equals("")) {
if (proxyAuthUser != null && !proxyAuthUser.equals("")) {
log("Proxy AuthUser: " + proxyAuthUser);
log("Proxy AuthPassword: " + proxyAuthPassword);
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication
getPasswordAuthentication() {
//respond only to proxy auth requests
if (getRequestorType().equals(RequestorType.PROXY)) {
return new PasswordAuthentication(proxyAuthUser,
proxyAuthPassword
.toCharArray());
} else {
return null;
}
}
});
}
final Proxy proxy = new Proxy(Type.HTTP, InetSocketAddress
.createUnresolved(proxyHost, proxyPort));
if(DEBUG){
log("Opening proxied connection(" + proxyHost + ":" + proxyPort + ")");
}
con = (HttpURLConnection) new URL(url).openConnection(proxy);
} else {
con = (HttpURLConnection) new URL(url).openConnection();
}
if (connectionTimeout > 0 && !isJDK14orEarlier) {
con.setConnectTimeout(connectionTimeout);
}
if (readTimeout > 0 && !isJDK14orEarlier) {
con.setReadTimeout(readTimeout);
}
return con;
}
示例14: testHttpResponseFromBrowser
@Test
public void testHttpResponseFromBrowser() throws URISyntaxException {
proxySelector.setBrowserResponse(new URI("http://" + PROXY_HOST + ":" + PROXY_PORT));
List<Proxy> result = proxySelector.select(new URI("http://example.org"));
Proxy expectedProxy = new Proxy(Type.HTTP, new InetSocketAddress(PROXY_HOST, PROXY_PORT));
assertNotNull(result);
assertEquals(1, result.size());
assertEquals(expectedProxy, result.get(0));
}
示例15: getProxy
private static Proxy getProxy ( final String url )
{
final ProxySelector ps = ProxySelector.getDefault ();
if ( ps == null )
{
logger.debug ( "No proxy selector found" );
return null;
}
final List<java.net.Proxy> proxies = ps.select ( URI.create ( url ) );
for ( final java.net.Proxy proxy : proxies )
{
if ( proxy.type () != Type.HTTP )
{
logger.debug ( "Unsupported proxy type: {}", proxy.type () );
continue;
}
final SocketAddress addr = proxy.address ();
logger.debug ( "Proxy address: {}", addr );
if ( ! ( addr instanceof InetSocketAddress ) )
{
logger.debug ( "Unsupported proxy address type: {}", addr.getClass () );
continue;
}
final InetSocketAddress inetAddr = (InetSocketAddress)addr;
return new Proxy ( Proxy.TYPE_HTTP, inetAddr.getHostString (), inetAddr.getPort () );
}
logger.debug ( "No proxy found" );
return null;
}