java访问asmx接口
Posted 女装大佬大老李
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java访问asmx接口相关的知识,希望对你有一定的参考价值。
网上找了很多例子,很多没法用,只有这个成功了记录一下,原链接忘了,大哥不好意思
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
工具类
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.SimpleHttpConnectionManager;
import org.apache.commons.httpclient.methods.PostMethod;
import java.io.IOException;
import java.util.Map;
public class SoapUtil
public static String callSoap(String url,String method, Map<String, String> paramMap) throws IOException
// 输入服务网址
HttpClient client = new HttpClient();
PostMethod post = new PostMethod(url+"/"+method);
// 设置参数
if (paramMap != null)
for (Map.Entry<String, String> item : paramMap.entrySet())
String key = item.getKey();
String value = item.getValue();
System.out.println(key + ":" + value);
post.setParameter(key, value);
// client.setTimeout(newTimeoutInMilliseconds);
// 执行,返回一个结果码
int code = client.executeMethod(post);
System.out.println("结果码:" + code);
// 获取xml结果
String result = post.getResponseBodyAsString();
System.out.println("结果:" + result);
// 释放连接
post.releaseConnection();
// 关闭连接
((SimpleHttpConnectionManager) client.getHttpConnectionManager()).shutdown();
return result;
调用
public static void main(String[] args) throws Exception
Map<String, String> paramMap = new HashMap<>();
paramMap.put("id", "10086");
paramMap.put("name", "老李");
SoapUtil.callSoap("http://120.79.xxx.xxx:1234/aaaa.asmx", "调用的方法", paramMap);
以上是关于java访问asmx接口的主要内容,如果未能解决你的问题,请参考以下文章