1
我想從運行在需要通過代理服務器訪問外部Web服務的環境中的playframework 2應用程序調用Web服務。在我application.conf文件我已經添加了以下屬性:通過HTTP代理使用WS調用Web服務
把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]
查看Play源代碼,http客戶端在沒有任何上下文的情況下被實例化,例如, lazy val client = new AsyncHttpClient()。我相信AsyncHttpClientConfig需要使用適當的代理上下文構建,然後作爲構造函數參數傳遞給AsynchHttpClient,以正確初始化客戶端以通過代理。 – 2012-04-25 03:01:30