有时候,在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. pku3670 Eating Together

    http://poj.org/problem?id=3670 DP,最长不降子序列,O(n*logn)解法 #include <stdio.h> #define N 30030 int n ...

  2. JSF 2 password example

    In JSF, you can use the <h:inputSecret /> tag to render a HTML input of type="password&qu ...

  3. shell脚本的入参

    shell脚本参数可以任意多,但只有前9个可以被访问,使用shift命令可以改变这个限制.参数从第一个开始,在第九个结束.$0 程序名字$n 第n个参数值,n=1..9 $* 所有命令行参数$@    ...

  4. Nuget~让包包带上自己的配置信息

    我们知道一般开发组件之后,组件都有相关配置项,最常见的作法就是把它写到web.config里,而如果你将这个文件直接放到nuget里打包,在进行安装包包时,会提示你这个文件已经存在,不能去覆盖原来的c ...

  5. Cocos2d-x 让精灵随手指移动起来二(简单实现)

    void HelloWorld::ccTouchMoved(cocos2d::CCTouch *touch, cocos2d::CCEvent *event) { CCSize winSize = C ...

  6. Win7 不能安装 msi 解决办法

    Win7 不能安装Setup.msi解决办法 解决方案如下: 新建一个文本文件,输入msiexec /i d:\Setup.msi (假设文件名为Setup.msi ,放在d盘根目录下,即是安装程序的 ...

  7. iOS GCD 与 NSOperationQueue

    NSOperationQueue ios NSOperation vs. GCD StackOverflow: NSOperation vs. Grand Central Dispatch Blog: ...

  8. 方法字段[C# 基础知识系列]专题二:委托的本质论

    首先声明,我是一个菜鸟.一下文章中出现技术误导情况盖不负责 引言: 上一个专题已和大家分享了我懂得的——C#中为什么须要委托,专题中简略介绍了下委托是什么以及委托简略的应用的,在这个专题中将对委托做进 ...

  9. 2015南阳CCPC C - The Battle of Chibi DP

    C - The Battle of Chibi Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Cao Cao made up a ...

  10. c# 路径空格---ProcessStartInfo参数问题

    今天在整合程序的时候,要从一个程序转到另一个程序 当然要使用:   ProcessStartInfo startInfo = new ProcessStartInfo("\\Program ...