有时候,在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. 编译arm64错误记录

    响应2月底appstore 64位APP的上线要求,开始编译IOS arm64版本引擎库.编译arm64遇到一些问题,在此记录. 1. 数据类型的错误 __int64 相关,提示error: expe ...

  2. 第二百六十八天 how can I坚持

    早上看了个电影<我的少女时代>,挺好看的. 下午从四点玩游戏一直玩到现在,也是疯了. 晚上也没有吃饭,是不是太堕落了. 徐斌他同学今天中午过来,做了个饭,也是服了,好难吃. 还没做好准备, ...

  3. work8

    使用裸指针: #include <iostream>#include <memory>#include <stdio.h>#include <cstring& ...

  4. HDU 4902 Nice boat (线段树)

    Nice boat 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4902 Description There is an old country a ...

  5. POJ 1251 && HDU 1301 Jungle Roads (最小生成树)

    Jungle Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/A http://acm.hust.edu.cn/vju ...

  6. HDU 5744 Keep On Movin (贪心)

    Keep On Movin 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5744 Description Professor Zhang has k ...

  7. WEB开发框架

  8. 转载IEnumerable与IEnumerator区别

    public interface IEnumerable {     IEnumerator GetEnumerator(); }   public interface IEnumerator {   ...

  9. vsftpd 权限设置

    vsftpd 虚拟用户 多用户不同权限 2010-06-27 00:54:20|  分类: linux大杂绘|举报|字号 订阅     1.需要建立一个用户,这个用户是linux系统的本地用户,各ft ...

  10. MPAndroidChart

    该库的可扩展性强,代码相对规范,最近一次更新有很大改进,如果不喜欢AChartEngine的过于复杂可以考虑在此库的基础上开发自己的图表类. linechart 填充式lineChart 单条线的Li ...