﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-Hafeyang's Tech Space-随笔分类-CXF</title><link>http://www.blogjava.net/Hafeyang/category/43542.html</link><description>Hafeyang目前从事销售领域系统开发，长于前端开发。乐意分享。</description><language>zh-cn</language><lastBuildDate>Tue, 12 Jan 2010 09:14:31 GMT</lastBuildDate><pubDate>Tue, 12 Jan 2010 09:14:31 GMT</pubDate><ttl>60</ttl><item><title>Spring+CXF配置HTTP代理访问Internet</title><link>http://www.blogjava.net/Hafeyang/archive/2010/01/12/309178.html</link><dc:creator>阳衡锋</dc:creator><author>阳衡锋</author><pubDate>Tue, 12 Jan 2010 09:10:00 GMT</pubDate><guid>http://www.blogjava.net/Hafeyang/archive/2010/01/12/309178.html</guid><wfw:comment>http://www.blogjava.net/Hafeyang/comments/309178.html</wfw:comment><comments>http://www.blogjava.net/Hafeyang/archive/2010/01/12/309178.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Hafeyang/comments/commentRss/309178.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Hafeyang/services/trackbacks/309178.html</trackback:ping><description><![CDATA[<p>一个典型的CXF Webservice项目部署到正式环境后，服务器不能访问外网，只能通过代理访问外网，找了很多资料，刚开始想法是不用Spring的配置文件，直接用java编程访问webservice。在java的Http请求中使用代理的方法如下：</p>
<pre class="code">String <span style="background: #f0d8a8">authentication=</span><span style="background: #f0d8a8; color: #2a00ff">"username:password"</span><span style="background: #f0d8a8">;</span><span style="background: #f0d8a8; color: #3f7f5f">//用户+&#8221;:&#8221;+密码
</span><span style="background: #f0d8a8">String encodedLogin= </span><span style="background: #f0d8a8; color: #7f0055">new </span><span style="background: #f0d8a8">BASE64Encoder().encode(</span><span style="background: #d4d4d4">authentication.getBytes());
Proxy proxy = </span><span style="background: #d4d4d4; color: #7f0055">new </span><span style="background: #d4d4d4">Proxy(Proxy.Type.</span><span style="background: #d4d4d4; color: #0000c0">HTTP</span><span style="background: #d4d4d4">, </span><span style="background: #d4d4d4; color: #7f0055">new </span><span style="background: #d4d4d4">InetSocketAddress(</span><span style="background: #d4d4d4; color: #2a00ff">"IP"</span><span style="background: #d4d4d4">, PORT));
HttpsURLConnection conn = (HttpsURLConnection) console.openConnection(proxy );
conn.setRequestProperty(</span><span style="background: #d4d4d4; color: #2a00ff">"Proxy-Authorization"</span><span style="background: #d4d4d4">, </span><span style="background: #d4d4d4; color: #2a00ff">" Basic " </span><span style="background: #d4d4d4">+ encodedLogin);</span></pre>
<p>使用JaxWsProxyFactoryBean创建client</p>
<pre class="code">System.setProperty(<span style="color: #2a00ff">"http.proxySet"</span>, <span style="color: #2a00ff">"true"</span>);
System.setProperty(<span style="color: #2a00ff">"http.proxyHost"</span>, <span style="color: #2a00ff">"IP"</span>);
System.setProperty(<span style="color: #2a00ff">"http.proxyPort"</span>, <span style="color: #2a00ff">"PORT"</span>);
<span style="background: #d4d4d4">JaxWsProxyFactoryBean f = </span><span style="background: #d4d4d4; color: #7f0055">new </span><span style="background: #d4d4d4">JaxWsProxyFactoryBean();
f.setAddress(</span><span style="background: #d4d4d4; color: #2a00ff">"THE URL OF WEBSERVICE"</span><span style="background: #d4d4d4">);
f.setServiceClass(IService.</span><span style="background: #d4d4d4; color: #7f0055">class</span><span style="background: #d4d4d4">);
IService client = (IService) f.create();</span></pre>
<p>可以使用代理，但是用户名密码是不能像第一种方法中那样设置到HttpConnection里面去的。</p>
<p>又在网上找了很多资料，终于找到了一个配置的方法。</p>
<pre class="code"><span style="color: teal">&lt;?</span><span style="color: #3f7f7f">xml </span><span style="color: #7f007f">version</span>=<span style="color: #2a00ff">"1.0" </span><span style="color: #7f007f">encoding</span>=<span style="color: #2a00ff">"UTF-8"</span><span style="color: teal">?&gt;
&lt;</span><span style="color: #3f7f7f">beans </span><span style="color: #7f007f">xmlns</span>=<span style="color: #2a00ff">"http://www.springframework.org/schema/beans"
</span><span style="color: #7f007f">xmlns:xsi</span>=<span style="color: #2a00ff">"http://www.w3.org/2001/XMLSchema-instance"
</span><span style="color: #7f007f">xmlns:cxf</span>=<span style="color: #2a00ff">"http://cxf.apache.org/core"
</span><span style="color: #7f007f">xmlns:sec</span>=<span style="color: #2a00ff">"http://cxf.apache.org/configuration/security"
</span><span style="color: #7f007f">xmlns:http-conf</span>=<span style="color: #2a00ff">"http://cxf.apache.org/transports/http/configuration"
</span><span style="color: #7f007f">xsi:schemaLocation</span>=<span style="color: #2a00ff">"
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
</span><span style="color: blue">http://cxf.apache.org/configuration/security
</span><span style="color: #2a00ff">http://cxf.apache.org/schemas/configuration/security.xsd
http://cxf.apache.org/transports/http/configuration
http://cxf.apache.org/schemas/configuration/http-conf.xsd"</span><span style="color: teal">&gt;
</span><span style="color: #3f5fbf">    </span><span style="color: teal">&lt;</span><span style="color: #3f7f7f">http-conf:conduit </span><span style="color: #7f007f">name</span>=<span style="color: #2a00ff">"*.http-conduit"</span><span style="color: teal">&gt;
&lt;</span><span style="color: #3f7f7f">http-conf:proxyAuthorization</span><span style="color: teal">&gt;
</span><span style="color: #3f5fbf">&lt;!-- 用户名 --&gt;
</span><span style="color: teal">&lt;</span><span style="color: #3f7f7f">sec:UserName</span><span style="color: teal">&gt;***</span><span style="color: teal">&lt;/</span><span style="color: #3f7f7f">sec:UserName</span><span style="color: teal">&gt;
</span><span style="color: #3f5fbf">&lt;!-- 密码 --&gt;
</span><span style="color: teal">&lt;</span><span style="color: #3f7f7f">sec:Password</span><span style="color: teal">&gt;</span>***<span style="color: teal">&lt;/</span><span style="color: #3f7f7f">sec:Password</span><span style="color: teal">&gt;
&lt;/</span><span style="color: #3f7f7f">http-conf:proxyAuthorization</span><span style="color: teal">&gt;
</span><span style="color: #3f5fbf">&lt;!--
ProxyServer IP                ProxyServerPort PORT
ProxyServerType: HTTP or SOCKS
--&gt;
</span><span style="color: teal">&lt;</span><span style="color: #3f7f7f">http-conf:client
</span><span style="color: #7f007f">ProxyServer</span>=<span style="color: #2a00ff">"192.168.1.4"
</span><span style="color: #7f007f">ProxyServerPort</span>=<span style="color: #2a00ff">"808"
</span><span style="color: #7f007f">ProxyServerType</span>=<span style="color: #2a00ff">"HTTP"
</span><span style="color: #7f007f">Connection</span>=<span style="color: #2a00ff">"Keep-Alive"
</span><span style="color: #7f007f">AllowChunking</span>=<span style="color: #2a00ff">"false"
</span><span style="color: #7f007f">ConnectionTimeout</span>=<span style="color: #2a00ff">"50000"
</span><span style="color: #7f007f">ReceiveTimeout</span>=<span style="color: #2a00ff">"120000"
</span><span style="color: teal">/&gt;
&lt;/</span><span style="color: #3f7f7f">http-conf:conduit</span><span style="color: teal">&gt;
</span><span style="color: #3f5fbf">&lt;!-- Client   --&gt;
</span><span style="color: teal">&lt;/</span><span style="color: #3f7f7f">beans</span><span style="color: teal">&gt;</span></pre>
<p><span style="color: teal">&lt;</span><span style="color: #3f7f7f">http-conf:conduit </span><span style="color: #7f007f">name</span>=<span style="color: #2a00ff">"*.http-conduit"</span><span style="color: teal">&gt; </span>这里的name为&#8221;*.http-conduit&#8221;时，将会对所有的client类启用这个代理，如果要配置某个client类使用代理，可以这么写 </p>
<p><a></a></p>
<a>
<pre>&lt;http-conf:conduit name="{http://widgets/widgetvendor.net}widgetSOAPPort.http-conduit&gt;
...
&lt;/http-conf:conduit&gt;</pre>
</a>
<p>{}里面的内容是webservice的wsdl的&lt;wsdl:definitions name="<strong>serviceName</strong>" targetNamespace="<strong><a href="http://hafeyang.blogjava.net">http://hafeyang.blogjava.net</a></strong>"的targetNamespace属性
</p>
<p>{}之后.之前的内容是
</p>
<p>&#8230;
</p>
<p>&lt;wsdl:port name="<strong>BasicHttpBinding_IService</strong>" binding="<strong>i0:BasicHttpBinding_IService</strong>"&gt;
</p>
<p>&lt;soap:address location="<strong>the address</strong>" /&gt;
</p>
<p>&lt;/wsdl:port&gt;
</p>
<p>&#8230;</p>
<p>的name属性。</p>
<p>上述wsdl对应的配置是</p>
<p>&lt;http-conf:conduit name="{http://hafeyang.blogjava.net}BasicHttpBinding_IService.http-conduit&gt; ... &lt;/http-conf:conduit&gt;</p>
<p>参考资料</p>
<p><a href="http://fusesource.com/docs/framework/2.2/bind_trans/HTTPConsumerConfig.html">http://fusesource.com/docs/framework/2.2/bind_trans/HTTPConsumerConfig.html</a></p>
<p><a href="http://topic.csdn.net/u/20090331/09/f3ce02e0-29be-40cc-a534-9f2e5ac23f9e.html">http://topic.csdn.net/u/20090331/09/f3ce02e0-29be-40cc-a534-9f2e5ac23f9e.html</a></p>
<p><a href="http://old.nabble.com/http:conduit-Proxy-td20462360.html">http://old.nabble.com/http:conduit-Proxy-td20462360.html</a></p>
<p><a href="http://mail-archives.apache.org/mod_mbox/servicemix-users/200807.mbox/%3C4885FCBB.5070506@gmail.com%3E">http://mail-archives.apache.org/mod_mbox/servicemix-users/200807.mbox/&lt;4885FCBB.5070506@gmail.com&gt;</a></p>
<img src ="http://www.blogjava.net/Hafeyang/aggbug/309178.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Hafeyang/" target="_blank">阳衡锋</a> 2010-01-12 17:10 <a href="http://www.blogjava.net/Hafeyang/archive/2010/01/12/309178.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>