初看这个错误,让我有点头大,因为我用客户端调用,它竟然告诉我服务器拒绝接收.
上网上看了很多资料(90%是英文).各说其词,结果还是无果而终.
终于功夫不负有心人,终于在不经意间看到了一个老外的陈词:
package cn.sss_grid.mis.action.gydq.service.client;
//import com.sss.util.JDom;
import java.util.ResourceBundle;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.soap.SOAP12Constants;
import org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
/**
*
* @author mark
*/
public class WebServiceClient {
static ResourceBundle rb = ResourceBundle.getBundle("cn.sss_grid.struts.ApplicationResources");
private static EndpointReference targetEPR =
new EndpointReference(rb.getString("webserviceid"));//(文件)
private static OMFactory fac = OMAbstractFactory.getOMFactory();
private static OMNamespace omNs = fac.createOMNamespace(rb.getString("namespace"), rb.getString("postfix"));//命名空间(文件)
private static ServiceClient sender ;//创建服器客户端
public static void main(String args[]){
WebServiceClient wc=new WebServiceClient();
String tag=wc.getTagList("*");
System.out.println(tag);
}
public WebServiceClient() {
try {
Options options = new Options();//设置传输参数
options.setSoapVersionURI(SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI);//设置soap调用的版本.
options.setTo(targetEPR);//设置目标
options.setTransportInProtocol(Constants.TRANSPORT_HTTP);//基于http的传输
//按这种初始化设置会出现403错误
options.setTimeOutInMilliSeconds(120000);//设置请求超时为2分钟
sender= new ServiceClient();
sender.setOptions(options);
//设置参数 ,初始化客户端
//如果报出axis2 forbidden 403.记得是服务器不能你访问了,把IIS的保持连接去掉就可以了.
} catch (AxisFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @see 构建Snapshot参数
* @param tagnames
* @return
*/
public static OMElement getSnapshotMethod(String tagnames) {
OMElement method = fac.createOMElement("GetSnapshot", omNs); //getMethod
OMElement tag = fac.createOMElement("tagnames", omNs);
tag.setText(tagnames);
method.addChild(tag);
return method;
}
/**
* @see 构建GetHistory参数
* @param TagMask
* @return
*/
public static OMElement getHistoryMethod(String tagnames,String starttime,String endtime,String interval) {
OMElement method = fac.createOMElement("GetHistory", omNs); //getMethod
OMElement tagnames_oe = fac.createOMElement("tagnames", omNs);
tagnames_oe.setText(tagnames);
method.addChild(tagnames_oe);
OMElement starttime_oe = fac.createOMElement("starttime", omNs);
starttime_oe.setText(starttime);
method.addChild(starttime_oe);
OMElement endtime_oe = fac.createOMElement("endtime", omNs);
endtime_oe.setText(endtime);
method.addChild(endtime_oe);
OMElement interval_oe = fac.createOMElement("interval", omNs);
interval_oe.setText(interval);
method.addChild(interval_oe);
return method;
}
/**
* @see 构建GetSummary参数
* @param TagMask
* @return
*/
public static OMElement getSummaryMethod(String tagnames,String starttime,String endtime,String titledesc) {
OMElement method = fac.createOMElement("GetSummary", omNs); //getMethod
OMElement tagnames_oe = fac.createOMElement("tagnames", omNs);
tagnames_oe.setText(tagnames);
method.addChild(tagnames_oe);
OMElement starttime_oe = fac.createOMElement("starttime", omNs);
starttime_oe.setText(starttime);
method.addChild(starttime_oe);
OMElement endtime_oe = fac.createOMElement("endtime", omNs);
endtime_oe.setText(endtime);
method.addChild(endtime_oe);
OMElement titledesc_oe = fac.createOMElement("titledesc", omNs);
titledesc_oe.setText(titledesc);
method.addChild(titledesc_oe);
return method;
}
/**
* @see 构建GetSummaryAll参数
* @param TagMask
* @return
*/
public static OMElement getSummaryAllMethod(String tagnames,String starttime,String endtime) {
OMElement method = fac.createOMElement("GetSummary", omNs); //getMethod
OMElement tagnames_oe = fac.createOMElement("tagnames", omNs);
tagnames_oe.setText(tagnames);
method.addChild(tagnames_oe);
OMElement starttime_oe = fac.createOMElement("starttime", omNs);
starttime_oe.setText(starttime);
method.addChild(starttime_oe);
OMElement endtime_oe = fac.createOMElement("endtime", omNs);
endtime_oe.setText(endtime);
method.addChild(endtime_oe);
return method;
}
/**
* @see 构建TagList参数
* @param TagMask
* @return
*/
public static OMElement getTagListMethod(String TagMask) {
OMElement method = fac.createOMElement("GetTagList", omNs); //getMethod
OMElement tag = fac.createOMElement("TagMask", omNs);
tag.setText(TagMask);
method.addChild(tag);
OMElement desc = fac.createOMElement("DescMask", omNs);
desc.setText("*");
method.addChild(desc);
OMElement pointSource = fac.createOMElement("PointSource", omNs);
pointSource.setText("*");
method.addChild(pointSource);//method add all param
return method;
}
/**
* @see 此方法为供其他方法调用的方法.返回一个String.
* @param TagMask
* @return
*/
public static String getTagList(String TagMask){
try {
OMElement result=sender.sendReceive(getTagListMethod(TagMask));
String response = result.getFirstElement().getText();
return response;
} catch (AxisFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
//GetSnapshot
public String getSnapshot(String tagnames) {
try {
OMElement result=sender.sendReceive(getSnapshotMethod(tagnames));
String response = result.getFirstElement().getText();
return response;
} catch (AxisFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
//GetHistory
public String getHistory(String tagnames,String starttime,String endtime,String interval) {
try {
OMElement result=sender.sendReceive(getHistoryMethod(tagnames,starttime,endtime,interval));
String response = result.getFirstElement().getText();
return response;
} catch (AxisFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
//GetSummary
public String getSummary(String tagnames,String starttime,String endtime, String titledesc) {
try {
OMElement result=sender.sendReceive(getSummaryMethod(tagnames,starttime,endtime,titledesc));
String response = result.getFirstElement().getText();
return response;
} catch (AxisFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
//
public String getSummaryAll(String tagnames,String starttime,String endtime) {
try {
OMElement result=sender.sendReceive(getSummaryAllMethod(tagnames,starttime,endtime));
String response = result.getFirstElement().getText();
return response;
} catch (AxisFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}