日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

cxf設(shè)置代理訪問webservice接口

 瀚海璨夜 2019-01-30

  由于業(yè)務(wù)上的需要,需要訪問第三方提供的webservice接口,但由于公司做了對外訪問的限制,不設(shè)置代理是不能外網(wǎng)的,如果使用http設(shè)置代理訪問外網(wǎng)還是比較容易的,但使用cxf有點不知道從哪里入手。網(wǎng)上也有一些零散的信息,現(xiàn)在我整理一下提供參考。

1、JaxWsProxyFactoryBean設(shè)置代理

復(fù)制代碼
import org.apache.cxf.configuration.security.ProxyAuthorizationPolicy;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;

public class TeacherService {
    
    public String getStudents(StudentCard card){
        JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();  
        factoryBean.setServiceClass(TeacherWebService.class);  
        factoryBean.setAddress("http://./webservice?wsdl");  
        TeacherWebService tService = (TeacherWebService )factoryBean.create();  
            
       Client client = ClientProxy.getClient(tService);
        HTTPConduit http = (HTTPConduit) client.getConduit();
        HTTPClientPolicy hcp = new HTTPClientPolicy();
        hcp.setProxyServer(proxyHost);
        hcp.setProxyServerPort(proxyport);
        http.setClient(hcp);

        ProxyAuthorizationPolicy proxyAuthorization = new ProxyAuthorizationPolicy();
        proxyAuthorization.setUserName(proxyUsername);
        proxyAuthorization.setPassword(proxyPassword);
        http.setProxyAuthorization(proxyAuthorization);
        
        String res = tService.getStudents(card);
        
        return res;
    }
}
復(fù)制代碼

2、使用JaxWsDynamicClientFactory設(shè)置代理

復(fù)制代碼
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import javax.xml.namespace.QName;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import com.thoughtworks.xstream.XStream;

public class TeacherService {
    
    public String getStudents(StudentCard card)throws Exception{
      System.setProperty("http.proxyHost","http.proxy.host");
        System.setProperty("http.proxyPort", "http.proxy.port");
        Authenticator.setDefault(new MyAuthenticator("username", "password")));
        
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();  
        
         //url為調(diào)用webService的wsdl地址  
        Client client = dcf.createClient("http://x.:8080/webservice?wsdl");  
        //namespace是命名空間,methodName是方法名 
        QName name=new QName("http://www.","getStudents");  
     XStream xstream = new XStream(new XppDriver(new XmlFriendlyNameCoder("_-", "_")));
        Object[] objects = client.invoke(name,xstream.toXML(card)); 
        
        String res = "";
        if(objects != null && objects.length != 0){
            res = objects[0].toString();
        }
        
        return res;
  }

  static class MyAuthenticator extends Authenticator {
    private String username, password;

    public MyAuthenticator(String username, String password) {
      this.username = username;
      this.password = password;
    }

    protected PasswordAuthentication getPasswordAuthentication() {
      return new PasswordAuthentication(username, password.toCharArray());
    }
  }

}
復(fù)制代碼

 

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多