到公司没多久,写了一个java调用.net写的webService结果期间用各种方法测试都没有完成,总是抛出异常,最后直接使用SOAP消息去进行调用才成功了,具体代码如下,仅供参考:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class QueryDataUtil {
public static String getOpdetailString(String regoinId,String regoinName,String signalName) throws Exception{
String xml ="";
xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
xml +="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:tem=\"http://tempuri.org/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">";
xml +="<soapenv:Header/>";
xml +="<soapenv:Body>";
xml += "<tem:QueryDeviceRealData><tem:regionId>"+regoinId+"</tem:regionId><tem:regionName>"+regoinName+"</tem:regionName><tem:signalName>"+signalName+"</tem:signalName></tem:QueryDeviceRealData>";
String xml1="</soapenv:Body>";
xml1+="</soapenv:Envelope>";
xml +=xml1;
String urlString="http://IP:端口/xxxx?wsdl";
HttpURLConnection httpConn = null;
OutputStream out = null;
String returnXml="";
//设置批次号
DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
String s= df.format(new Date());
System.out.println("开始");
httpConn = (HttpURLConnection) new URL(urlString).openConnection();
httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
httpConn.setRequestProperty("SOAPAction", "xxx");
httpConn.setRequestMethod("POST");
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.connect();
out = httpConn.getOutputStream(); // 获取输出流对象
httpConn.getOutputStream().write(xml.getBytes()); // 将要提交服务器的SOAP请求字符流写入输出流
out.flush();
out.close();
int code = httpConn.getResponseCode(); // 用来获取服务器响应状态
String tempString = null;
StringBuffer sb1 = new StringBuffer();
if (code == HttpURLConnection.HTTP_OK) {
BufferedReader reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), "UTF-8"));
while ((tempString = reader.readLine()) != null) {
sb1.append(tempString);
}
if (null != reader) {
reader.close();
}
} else {
BufferedReader reader = new BufferedReader(new InputStreamReader(httpConn.getErrorStream(), "UTF-8"));
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
sb1.append(tempString);
}
if (null != reader) {
reader.close();
}
}
//响应报文
returnXml=sb1.toString();
System.out.println("returnOpdetail==="+returnXml);
return returnXml;
}
}
望大家有什么意见多多提议哈