有时候,在java下开发会调用一下.net下写的服务,看网上有各种方法,但总是不成功,总结下自己测试过能调用成功的方式:

1. 原始方式http-soap

public static String postWs() throws IOException {
OutputStreamWriter out = null;
StringBuilder sTotalString = new StringBuilder();
String soapStr=genSoapXml();
try {
URL urlTemp = new URL("http://10.6.54.238:8005/SendMessageService.asmx");
HttpURLConnection connection = (HttpURLConnection)urlTemp.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
//connection.setRequestProperty("Host", "10.6.54.238");
connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
//connection.setRequestProperty("Content-Length", Integer.toString(soapStr.length()));
connection.setRequestProperty("SOAPAction","http://ilink.***.com.cn/SendMessage"); out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
out.write(soapStr);
out.flush(); String sCurrentLine;
InputStream l_urlStream= connection.getInputStream();
BufferedReader l_reader = new BufferedReader(new InputStreamReader(l_urlStream));
while ((sCurrentLine = l_reader.readLine()) != null) {
sTotalString.append(sCurrentLine);
} } finally {
if (null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sTotalString.toString();
} static String genSoapXml(){
/*String str="<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n" +
" <soap12:Body>\n" +
" <SendMessage xmlns=\"http://ilink.***.com.cn/\">\n" +
" <strRequest></strRequest>\n" +
" </SendMessage>\n" +
" </soap12:Body>\n" +
"</soap12:Envelope>";*/
StringBuilder sb=new StringBuilder();
sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
sb.append("<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/\">" );
sb.append("<soap:Body>");
sb.append("<SendMessage xmlns=\"http://ilink.***.com.cn/\">");
sb.append("<strRequest><![CDATA["+genReq()+"]]></strRequest>" );
sb.append("</SendMessage>" );
sb.append("</soap:Body>" );
sb.append("</soap:Envelope>");
return sb.toString();
}

红色斜体部分是调用方法和参数

2. 使用apache axis,看起来特优雅

添加maven依赖

<dependency>
  <groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency> <dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis-jaxrpc</artifactId>
<version>1.4</version>
</dependency> <dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.3</version>
</dependency> <dependency>
<groupId>commons-discovery</groupId>
<artifactId>commons-discovery</artifactId>
<version>0.5</version>
</dependency>
public static void testAsmx() throws RemoteException, ServiceException, MalformedURLException {

        Map<String, String> params = new HashMap<String, String>();
params.put("strRequest", genReq()); String result = callAsmxWebService("http://10.6.54.238:8005/SendMessageService.asmx", "http://ilink.***.com.cn/", "SendMessage", params);
logger.error("asmx:"+result);
} static String callAsmxWebService(String serviceUrl, String serviceNamespace,
String methodName, Map<String, String> params) throws ServiceException, MalformedURLException, RemoteException { org.apache.axis.client.Service service = new org.apache.axis.client.Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(serviceUrl));
call.setOperationName(new QName(serviceNamespace,methodName)); ArrayList<String> paramValues = new ArrayList<String>();
for (Map.Entry<String, String> entry : params.entrySet()) {
call.addParameter(new QName(serviceNamespace, entry.getKey()),
XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
paramValues.add(entry.getValue());
} call.setReturnType(XMLType.XSD_STRING);
call.setUseSOAPAction(true);
call.setSOAPActionURI(serviceNamespace + methodName); return (String) call.invoke(new Object[] { paramValues.toArray() }); }

3. 使用httpclient4.5.jar,也是post soap xml

public static String testWs() throws IOException {
String serviceUrl = "http://10.6.54.238:8005/SendMessageService.asmx";
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(serviceUrl); String str=genSoapXml(); httpPost.setEntity(new StringEntity(str, "text/xml;", HTTP.UTF_8));
HttpResponse httpResponse = client.execute(httpPost); int code=httpResponse.getStatusLine().getStatusCode();      if(200 ==code){
  HttpEntity entity = httpResponse.getEntity();
  InputStream inputStream = entity.getContent();
  byte[] contentBytes = IOUtils.toByteArray(inputStream);
  String res = new String(contentBytes, HTTP.UTF_8);
       inputStream.close();
       return res;
     }
     return null;
}

java调用.net asmx服务的更多相关文章

  1. Java调用Webservice(asmx)的几个例子

    Java调用Webservice(asmx)的几个例子 2009-06-28 17:07 写了几个调用例子: 1. import org.apache.axis.client.*;import org ...

  2. java调用.net asmx / wcf

    一.先用asmx与wcf写二个.net web service: 1.1 asmx web服务:asmx-service.asmx.cs using System; using System.Coll ...

  3. java 调用webservice (asmx) 客户端开发示例

    这是本人第一次写博客,其实就是自己做个笔记,写的很粗糙,也希望能给跟我遇到同样问题的你一点帮助. 因为最近有个项目要调用webservice接口,之前接触的都是Java开发服务端和客户端的接口,开发前 ...

  4. Java 调用Azure认知服务Demo--Computer API

    说明 本文主要介绍使用Java代码,基于HTTP请求调用Microsoft Azure的认知服务.图片来源分别介绍了使用公网的URL和上传本地图片. 依赖的jar包下载地址: key的获取需要登录到A ...

  5. java 调用打印机 打印服务

    import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import javax ...

  6. 调用URL 接口服务

    1.Net调用URL 接口服务 using System; using System.Collections; using System.Configuration; using System.Dat ...

  7. JAVA与.NET的相互调用——通过Web服务实现相互调用

    JAVA与.NET是现今世界竞争激烈的两大开发媒体,两者语言有很多相似的地方.而在很多大型的开发项目里面,往往需要使用两种语言进行集成开发.而很多的开发人员都会偏向于其中一种语言,在使用集成开发的时候 ...

  8. Java调用Http/Https接口(1)--编写服务端

    Http接口输入的数据一般是键值对或json数据,返回的一般是json数据.本系列文章主要介绍Java调用Http接口的各种方法,本文主要介绍服务端的编写,方便后续文章里的客户端的调用.文中所使用到的 ...

  9. java调用mysql服务做备份与恢复

    首先添加mysql的bin到环境变量,这样可以简写部分命令,并且做到不依赖系统mysql的具体安装路径. 重启计算机可以让添加的环境变量在java代码中调用时生效.(cmd中生效但java中调用没有生 ...

随机推荐

  1. MySQL存储过程权限分析

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://huanghualiang.blog.51cto.com/6782683/1216 ...

  2. DAG的生成

    DAG的生成 原始的RDD(s)通过一系列转换就形成了DAG.RDD之间的依赖关系,包含了RDD由哪些Parent RDD(s)转换而来和它依赖parent RDD(s)的哪些Partitions,是 ...

  3. Yii CModel中rules验证规则

    array( array(‘username’, ‘required’), array(‘username’, ‘length’, ‘min’=>3, ‘max’=>12), array( ...

  4. MongoDB 字段增长

    MongoDB中存储的文档必须有一个"_id"键.这个键的值可以是任何类型的,默认是个ObjectId对象. ObjectId 是一个12字节 BSON 类型数据,有以下格式: 前 ...

  5. CodeForces 682E Alyona and Triangles (计算几何)

    Alyona and Triangles 题目连接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/J Description You ar ...

  6. java 字符串String操作工具类

    /** * StrKit. */ public class StrKit { /** * 首字母变小写 */ public static String firstCharToLowerCase(Str ...

  7. POJ 1679 The Unique MST (次小生成树)

    题目链接:http://poj.org/problem?id=1679 有t组数据,给你n个点,m条边,求是否存在相同权值的最小生成树(次小生成树的权值大小等于最小生成树). 先求出最小生成树的大小, ...

  8. js两种创建对象方式

    js创建方法的两种方式 <%@ page language="java" contentType="text/html; charset=ISO-8859-1&qu ...

  9. IT职场生存法则

    转!!!!!!!!!!!!! 摘要我在IT职场打滚超过15年了,从小小的程序员做到常务副总.相对于其它行业,IT职场应该算比较光明的了,但也陷阱重重,本文说说我的亲身体会,希望大家能在IT职场上战无不 ...

  10. CentOS 6系统下安装 JDK1.6

    CentOS 6系统下安装 JDK1.6 JDK(Java Development Kit)是Sun Microsystems针对Java开发员的产品.自从Java推出以来,JDK已经成为使用最广泛的 ...