Spring+CXF配置HTTP代理訪問Internet

轉:http://www.blogjava.net/Hafeyang/archive/2010/01/12/309178.html
Spring+CXF配置HTTP代理訪問Internet

一個典型的CXF Webservice項目部署到正式環境後,服務器不能訪問外網,只能通過代理訪問外網,找了很多資料,剛開始想法是不用Spring的配置文件,直接用java編程訪問webservice。在java的Http請求中使用代理的方法如下:

String authentication="username:password";//用戶+”:”+密碼
String encodedLogin= new BASE64Encoder().encode(authentication.getBytes());
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("IP", PORT));

HttpsURLConnection conn = (HttpsURLConnection) console.openConnection(proxy );
conn.setRequestProperty("Proxy-Authorization", " Basic " + encodedLogin);

使用JaxWsProxyFactoryBean創建client

System.setProperty("http.proxySet", "true");
System.setProperty("http.proxyHost", "IP");
System.setProperty("http.proxyPort", "PORT");

JaxWsProxyFactoryBean f = new JaxWsProxyFactoryBean();
f.setAddress("THE URL OF WEBSERVICE");
f.setServiceClass(IService.class);
IService client = (IService) f.create();

可以使用代理,但是用戶名密碼是不能像第一種方法中那樣設置到HttpConnection裏面去的。

又在網上找了很多資料,終於找到了一個配置的方法。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cxf="http://cxf.apache.org/core"
xmlns:sec="http://cxf.apache.org/configuration/security"
xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://cxf.apache.org/configuration/security
http://cxf.apache.org/schemas/configuration/security.xsd
http://cxf.apache.org/transports/http/configuration
http://cxf.apache.org/schemas/configuration/http-conf.xsd">
<http-conf:conduit name="*.http-conduit">
<http-conf:proxyAuthorization>

<!-- 用戶名 -->
<sec:UserName>***</sec:UserName>
<!-- 密碼 -->
<sec:Password>***</sec:Password>

</http-conf:proxyAuthorization>
<!--
ProxyServer IP ProxyServerPort PORT
ProxyServerType: HTTP or SOCKS
-->
<http-conf:client
ProxyServer="192.168.1.4"
ProxyServerPort="808"
ProxyServerType="HTTP"
Connection="Keep-Alive"
AllowChunking="false"
ConnectionTimeout="50000"
ReceiveTimeout="120000"
/>
</http-conf:conduit>

<!-- Client -->
</beans>

<http-conf:conduit name="*.http-conduit"> 這裏的name爲”*.http-conduit”時,將會對所有的client類啓用這個代理,如果要配置某個client類使用代理,可以這麼寫

<http-conf:conduit name="{http://widgets/widgetvendor.net}widgetSOAPPort.http-conduit>
...
</http-conf:conduit>

{}裏面的內容是webservice的wsdl的<wsdl:definitions name="serviceName" targetNamespace="http://hafeyang.blogjava.net"的targetNamespace屬性

{}之後.之前的內容是



<wsdl:port name="BasicHttpBinding_IService" binding="i0:BasicHttpBinding_IService">

<soap:address location="the address" />

</wsdl:port>


的name屬性。

上述wsdl對應的配置是

<http-conf:conduit name="{http://hafeyang.blogjava.net}BasicHttpBinding_IService.http-conduit> ... </http-conf:conduit>

參考資料

http://fusesource.com/docs/framework/2.2/bind_trans/HTTPConsumerConfig.html

http://topic.csdn.net/u/20090331/09/f3ce02e0-29be-40cc-a534-9f2e5ac23f9e.html

http://old.nabble.com/http:conduit-Proxy-td20462360.html

http://mail-archives.apache.org/mod_mbox/servicemix-users/200807.mbox/<[email protected]>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章