【问题标题】:Android Spring RestTemplate with Proxy带有代理的 Android Spring RestTemplate
【发布时间】:2011-11-09 21:02:55
【问题描述】:

我需要从手机读取代理信息,如果手机在代理下,我需要在我的 spring restTemplate 对象上设置这个代理。我已经尝试了以下代码,但没有任何运气,谁能告诉我我是什么我做错了。

String proxyHost = android.net.Proxy.getHost(activity
            .getApplicationContext());

SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, 8080));
requestFactory.setProxy(proxy);
RestTemplate rest = new RestTemplate(requestFactory);

感谢您的帮助

【问题讨论】:

    标签: android spring proxy spring-android


    【解决方案1】:

    我已经找到了我的问题的答案.. 将在此处发布代码:

     if (proxyHost != null && proxyPort != null) {
            rest = new RestTemplate();
            HttpComponentsClientHttpRequestFactory factory =   ((HttpComponentsClientHttpRequestFactory) rest
                    .getRequestFactory());
            DefaultHttpClient defaultHttpClient = (DefaultHttpClient) factory
                    .getHttpClient();
            HttpHost proxy = new HttpHost(proxyHost, proxyPort);
            defaultHttpClient.getParams().setParameter(
                    ConnRoutePNames.DEFAULT_PROXY, proxy);
    
        } else {
            rest = new RestTemplate();
            HttpComponentsClientHttpRequestFactory factory = ((HttpComponentsClientHttpRequestFactory) rest
                    .getRequestFactory());
            DefaultHttpClient defaultHttpClient = (DefaultHttpClient) factory
                    .getHttpClient();
    
        }
    

    【讨论】:

    • defaultHttpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);!谢谢你,你让我很开心!我试图将需要 HTTP 基本身份验证的服务连接与使用代理的要求结合起来,这最终允许我这样做。
    猜你喜欢
    • 2023-03-05
    • 2011-12-27
    • 1970-01-01
    • 2016-04-11
    • 2018-02-01
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多