xfire 實現https調用webservice 服務

package com.nciic.client;


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Proxy;
import java.net.MalformedURLException;


import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
import org.codehaus.xfire.client.Client;
import org.codehaus.xfire.client.XFireProxy;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import org.codehaus.xfire.transport.http.CommonsHttpMessageSender;
import org.codehaus.xfire.transport.http.EasySSLProtocolSocketFactory;
import org.codehaus.xfire.util.dom.DOMOutHandler;


import com.nciic.serv.ServiceInf;


public class NciicClient {

public static final String SERVICE_URL = "https://ws.nciic.org.cn/nciic_ws/services/";
public NciicClient() {
}
public static void main(String[] args) throws MalformedURLException {

try{


String license = ""; 
  
         String  inConditions = "";


new NciicClient().executeClient("NciicServices", license , inConditions);


}catch(Exception e){
e.printStackTrace();
}
}
public String executeClient(String serviceName, String license, String condition)
throws MalformedURLException {
long time = System.currentTimeMillis();
//調用生產環境接口時,需要採用https
ProtocolSocketFactory easy = new EasySSLProtocolSocketFactory();
Protocol protocol = new Protocol("https", easy, 443);
Protocol.registerProtocol("https", protocol);
//--核心服務NciicServices
Service serviceModel = new ObjectServiceFactory().create(ServiceInf.class, "NciicServices", null, null);
ServiceInf service = (ServiceInf) new XFireProxyFactory().create(serviceModel, SERVICE_URL + serviceName);
Client client = ((XFireProxy) Proxy.getInvocationHandler(service)).getClient();
client.addOutHandler(new DOMOutHandler());
client.setProperty(CommonsHttpMessageSender.GZIP_ENABLED, Boolean.TRUE);
// 忽略超時
client.setProperty(CommonsHttpMessageSender.HTTP_TIMEOUT, "0");
String result = null;
try {

/**
* 調用方法
*/
result = service.nciicCheck(license, condition);

System.out.println( result);
} catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
time = System.currentTimeMillis() - time;
System.out.println("------out time -----"+time);
return result;
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章