4

环境:MAC Proc

在使用 MonoTouch、HttpClient 和 Charles Proxy 时查看了 HTTP 流量监控问题

虽然我的问题与它类似,但我的应用程序正在使用RestTemplate http://docs.spring.io/autorepo/docs/spring-android/1.0.x/reference/html/rest-template.html。每当通过 Web 浏览器(特别是 Safari 和 Chrome)发出请求时,我都能够捕获 HTTP 和 HTTPS 流量,而当我通过我的应用程序(基本上使用 RestTemplate)发出休息请求时,Charles 代理没有捕获它们。

从以上问题的回答来看,应该是RestTempate上的设置代理设置相关的东西。但是我是一个 .NET 开发人员并且非常熟悉提琴手——但最近转移到 Java 并且对 Charles 来说是新的——所以如果你能告诉我具体的细节,或者你对配置 rest 模板以使用我的 mac os 代理的想法,那就太好了。 ..

基本上问题是如何配置我的休息模板(客户端),以便 charles Web 代理能够通过我的应用程序(客户端)捕获请求和响应。

其余模板由扩展 __https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/client/ SimpleClientHttpRequestFactory .html 的自定义请求工厂实例化。


看起来我需要做类似的事情(参考:http ://docs.spring.io/spring-integration/reference/html/http.html#http-proxy )

<bean id="requestFactory"
    class="org.springframework.http.client.SimpleClientHttpRequestFactory">
    <property name="proxy">
        <bean id="proxy" class="java.net.Proxy">
            <constructor-arg>
                <util:constant static-field="java.net.Proxy.Type.HTTP"/>
            </constructor-arg>
            <constructor-arg>
                <bean class="java.net.InetSocketAddress">
                    <constructor-arg value="123.0.0.1"/>
                    <constructor-arg value="8080"/>
                </bean>
            </constructor-arg>
        </bean>
    </property>
</bean>
4

2 回答 2

2

我也有类似的问题。但我使用的是 JUnit 和核心 Java。下面的链接在某些情况下对我有帮助。

https://www.charlesproxy.com/documentation/configuration/browser-and-system-configuration/

你可以去JAVA APPLICATION PROXY CONFIGURATION找到答案。

您可以将 Java 应用程序配置为在代码中使用 Charles 或作为 java 可执行文件的命令行参数。

System.setProperty("http.proxyHost", "127.0.0.1");

System.setProperty("http.proxyPort", "8888");

对于 HTTPS 也是如此。请注意,在这种情况下,您可能还希望将 Java 配置为信任 Charles 的根证书(请参阅 SSL 代理)。

于 2016-06-17T07:13:46.727 回答
0

你不能在 tomcat 级别这样做,你必须在 RestTemplate 中配置代理。参考这个https://stackoverflow.com/a/45188490/1743046,虽然它使用 HttpClient 但我认为你可以为 RestTemplate 找到相同的方法

于 2019-09-27T07:42:55.233 回答