java调用.net asmx服务
有时候,在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服务的更多相关文章
- Java调用Webservice(asmx)的几个例子
Java调用Webservice(asmx)的几个例子 2009-06-28 17:07 写了几个调用例子: 1. import org.apache.axis.client.*;import org ...
- java调用.net asmx / wcf
一.先用asmx与wcf写二个.net web service: 1.1 asmx web服务:asmx-service.asmx.cs using System; using System.Coll ...
- java 调用webservice (asmx) 客户端开发示例
这是本人第一次写博客,其实就是自己做个笔记,写的很粗糙,也希望能给跟我遇到同样问题的你一点帮助. 因为最近有个项目要调用webservice接口,之前接触的都是Java开发服务端和客户端的接口,开发前 ...
- Java 调用Azure认知服务Demo--Computer API
说明 本文主要介绍使用Java代码,基于HTTP请求调用Microsoft Azure的认知服务.图片来源分别介绍了使用公网的URL和上传本地图片. 依赖的jar包下载地址: key的获取需要登录到A ...
- java 调用打印机 打印服务
import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import javax ...
- 调用URL 接口服务
1.Net调用URL 接口服务 using System; using System.Collections; using System.Configuration; using System.Dat ...
- JAVA与.NET的相互调用——通过Web服务实现相互调用
JAVA与.NET是现今世界竞争激烈的两大开发媒体,两者语言有很多相似的地方.而在很多大型的开发项目里面,往往需要使用两种语言进行集成开发.而很多的开发人员都会偏向于其中一种语言,在使用集成开发的时候 ...
- Java调用Http/Https接口(1)--编写服务端
Http接口输入的数据一般是键值对或json数据,返回的一般是json数据.本系列文章主要介绍Java调用Http接口的各种方法,本文主要介绍服务端的编写,方便后续文章里的客户端的调用.文中所使用到的 ...
- java调用mysql服务做备份与恢复
首先添加mysql的bin到环境变量,这样可以简写部分命令,并且做到不依赖系统mysql的具体安装路径. 重启计算机可以让添加的环境变量在java代码中调用时生效.(cmd中生效但java中调用没有生 ...
随机推荐
- Juicer javascript 模板引擎
模板引擎是为了使用户界面与业务数据(内容)分离而产生的,它可以生成特定格式的文档,用于网站的模板引擎就会生成一个标准的HTML文档. js模板引擎包括如下:template官方参考:http://au ...
- JavaFX 2 Dialogs
http://edu.makery.ch/blog/2012/10/30/javafx-2-dialogs/ ———————————————————————————————————————————— ...
- 查杀oracle锁表
()锁表查询的代码有以下的形式: select count(*) from v$locked_object; select * from v$locked_object; ()查看哪个表被锁 sele ...
- DevExpress.XtraGrid的使用(部分)
(三) XtraGrid的功能与GridView的功能相似,使用方法也类似,不过他的功能更加令人惊讶,看看下面的效果 Code//生成此效果的解决方案: // 1.从数据库去出两张表, //后台 Sq ...
- vs2012下安装VisualHG
好久没写东西了.懒了.最近开发用到HG,记录一下.希望对用这个的有用 http://visualhg.codeplex.com/ 这里下载 VisualHG 安装完默认情况下 源代码管理出不来Visu ...
- cocos2d-x 读取.plist文件
转自:http://blog.csdn.net/hgplan/article/details/8629904 在cocos2d-x中可以用.plist格式的文件来保存数据,它是XML文件格式的一种,在 ...
- 基础数据结构 之 队列(python实现)
队也是编程开发中常见的一种数据结构.栈和队可用来模拟函数的递归过程.队的特点为先入先出,主要操作包括入队和出队.入队时需判断队是否已满,出队时需判断队是否为空.下面给出一个队的python实现的例子: ...
- 从零开始学android开发- layout属性介绍
android:id 为控件指定相应的ID android:text 指定控件当中显示的文字,需要注意的是,这里尽量使用strings.xml文件当中的字符串 android:gravity 指定Vi ...
- Codeforces Round #185 (Div. 2) C. The Closest Pair 构造
C. The Closest Pair Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/312/p ...
- HDU 5536 Chip Factory 字典树
Chip Factory Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...