java 中用Axis访问C#的webservice接口

自己随便整理的 ,写得不好请见谅


方法一:

要遵守axis的一些语法规则,它内部做了封装处理,只要传指定参数即可

引入的jar包:
axis.jar    
commons-digester-2.0.jar  
 commons-discovery-0.2.jar  
 commons-logging-1.1.1.jar  
  wsdl4j-1.6.2.jar    
 jaxrpc.jar

/**
* 接口URL、域名、接口参数Object[]数组、接口方法名、接口参数名()List<String>集合----如无参数
* 可传递空Object[]数组 返回信息 Object
* */
        //url --   web服务地址
       //soapaction  --web服务命名空间
     //City   ---参数值列表
     //method---函数名
    //parameter -参数名称列表  

public Object webServiceAsmx(String url,String soapaction,Object[] City,String method,List<String>  parameter,List<QName> qNames){
Object obj = null;
Service service = new Service();
int i = 0;
try {
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(url));
call.setOperationName(new QName(soapaction, method)); // 设置要调用哪个方法
for (; i < parameter.size(); i++) {
call.addParameter(new QName(soapaction, parameter.get(i)),
qNames.get(i),
javax.xml.rpc.ParameterMode.IN);
}
call.setUseSOAPAction(true);
call.setSOAPActionURI(soapaction + method);
if (i != 0) {//指定返回类型 根据ASMX返回类型指定
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_SCHEMA);//返回类型是复杂类型Xml格式的值时
if(call.invoke(City)!=null){
obj = (Schema) call.invoke(City);
}
} else {//无参数输入调用 根据ASMX返回类型指定 
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_SCHEMA);
obj = (String) call.invoke(City);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return obj;
}



方法二:

***使用原生态的方法,发送soap请求请求,直接获取响应信息****


public static  byte[] executeAction(String url, String method) throws IOException {
//HttpTimeoutHandler xHTH = new HttpTimeoutHandler(1000*1800);       
//URL target = new URL((URL)null, url, xHTH);
URL target = new URL(url);      //web服务地址
HttpURLConnection conn = (HttpURLConnection) target.openConnection();//发送这个url
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod(method);//请求方式 POST
conn.setRequestProperty("Content-Type", " text/xml; charset=utf-8");
conn.setRequestProperty("SOAPAction","http://tempuri.org/GetInfo");//命名空间/函数名

//请求内容:参考axmx页面下的具体函数的WSDL文档
String s ="<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body> <GeInfo xmlns=\"http://tempuri.org/\"><statDate>2015-01-02</statDate><IPStr>192.168.2.63</IPStr> </GeInfo></soap:Body></soap:Envelope>";

PrintWriter out = null;

if (s != null) {
int dataLength = s.length();
conn.setRequestProperty("Content-Length", Integer.toString(dataLength));
conn.setFixedLengthStreamingMode(dataLength);
// OutputStream out = conn.getOutputStream();
// out.write(request);
// out.flush();
// out.close();
out = new PrintWriter(conn.getOutputStream());
out.print(s);
out.flush();
out.close();
} else {
conn.connect();
}
int code = conn.getResponseCode();//响应号
boolean success = (code >= 200) && (code < 300);

InputStream in = success ? conn.getInputStream() : conn.getErrorStream();
try { 
int size = conn.getContentLength();
String sizeStr = conn.getHeaderField("content-length");
byte[] response = new byte[size];
int curr = 0, read = 0;

while (curr < size) {
read = in.read(response, curr, size - curr);
if (read <= 0) break;
curr += read;
}

if (size != curr) {
throw new IOException("Inconsistent length of data read while accessing: " + url);
}

if ( !success ) {
}

return response;
} finally {
if(in != null) {
in.close();
}
}
}





两种方式都不错,自己选择一种方式吧

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值