java调用.net asmx服务

时间:2023-03-09 03:41:10
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;
}