通过HTTP代理使用WS调用Web服务

时间:2012-04-20 09:15:44

标签: web-services proxy playframework-2.0

我正在尝试从我需要通过代理服务器访问外部Web服务的环境中运行的playframework 2应用程序调用Web服务。在我的application.conf文件中,我添加了以下属性:

把http.proxyPort = 8000

http.proxyHost = myproxyhost.innitech.com

在我的播放应用程序代码中,我可以访问系统属性并验证它们已被拾取。但是,在运行时,未使用代理配置。如果我在不需要使用代理来访问服务的环境中运行应用程序,则服务调用可以正常工作。

是否可以使用播放框架通过代理调用Web服务,如果是,如何调用?

以下是电话会议的一个例子:

def watched(username: String, password: String): Promise[Seq[Repo]] = {
  val promiseResponse = WS.url(baseUrl + "/user/watched")
    .withAuth(username, password, com.ning.http.client.Realm.AuthScheme.BASIC)
    .get

    promiseResponse.map(_.json.as[Seq[Repo]])
}

以下堆栈跟踪代表我在运行时看到的内容:

play.core.ActionInvoker$$anonfun$receive$1$$anon$1: Execution exception [[Connec
tException: https://api.github.com/user]]
    at play.core.ActionInvoker$$anonfun$receive$1.apply(Invoker.scala:82) [p
lay_2.9.1.jar:2.0]
    at play.core.ActionInvoker$$anonfun$receive$1.apply(Invoker.scala:63) [p
lay_2.9.1.jar:2.0]
    at akka.actor.Actor$class.apply(Actor.scala:290) [akka-actor.jar:2.0]
    at play.core.ActionInvoker.apply(Invoker.scala:61) [play_2.9.1.jar:2.0]
    at akka.actor.ActorCell.invoke(ActorCell.scala:617) [akka-actor.jar:2.0]

    at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:179) [akka-actor.jar:2.0]
Caused by: java.net.ConnectException: https://api.github.com/user
    at com.ning.http.client.providers.netty.NettyConnectListener.operationCo
mplete(NettyConnectListener.java:100) ~[async-http-client.jar:na]
    at org.jboss.netty.channel.DefaultChannelFuture.notifyListener(DefaultCh
annelFuture.java:397) ~[netty.jar:na]
    at org.jboss.netty.channel.DefaultChannelFuture.addListener(DefaultChann
elFuture.java:143) ~[netty.jar:na]
    at com.ning.http.client.providers.netty.NettyAsyncHttpProvider.doConnect
(NettyAsyncHttpProvider.java:1004) ~[async-http-client.jar:na]
    at com.ning.http.client.providers.netty.NettyAsyncHttpProvider.execute(N
ettyAsyncHttpProvider.java:829) ~[async-http-client.jar:na]
    at com.ning.http.client.AsyncHttpClient.executeRequest(AsyncHttpClient.j
ava:499) ~[async-http-client.jar:na]
Caused by: java.nio.channels.UnresolvedAddressException: null
    at sun.nio.ch.Net.checkAddress(Net.java:30) ~[na:1.6.0_25]
    at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:480) ~[na
:1.6.0_25]
    at org.jboss.netty.channel.socket.nio.NioClientSocketPipelineSink.connec
t(NioClientSocketPipelineSink.java:142) ~[netty.jar:na]
    at org.jboss.netty.channel.socket.nio.NioClientSocketPipelineSink.eventS
unk(NioClientSocketPipelineSink.java:105) ~[netty.jar:na]
    at org.jboss.netty.handler.ssl.SslHandler.handleDownstream(SslHandler.ja
va:459) ~[netty.jar:na]
    at org.jboss.netty.handler.codec.oneone.OneToOneEncoder.handleDownstream
(OneToOneEncoder.java:55) ~[netty.jar:na]

1 个答案:

答案 0 :(得分:2)

github中的当前快照应解决此问题,因为它使用Play上下文构建AsyncHttpClientConfig,然后使用config初始化AsynchHttpClient:

lazy val client = {
  import play.api.Play.current
  val config = new AsyncHttpClientConfig.Builder()       
    .setConnectionTimeoutInMs(current.configuration.getMilliseconds("ws.timeout").getOrElse(120000L).toInt)
    .setFollowRedirects(current.configuration.getBoolean("ws.followRedirects").getOrElse(true))
    .setUseProxyProperties(current.configuration.getBoolean("ws.useProxyProperties").getOrElse(true))

  current.configuration.getString("ws.useragent").map { useragent =>
    config.setUserAgent(useragent)
}
new AsyncHttpClient(config.build())

}