部分代码如下:
public class DomXMLString {
private static String SERVICES_HOST = "www.webxml.com.cn";
private static String NETDATA_URL = "http://183.131.242.70:8089/ship/interface/shipInfoService.asmx/getTask";
private static String LOCAL_PC_SAVEFILE_URL = "E:Myeclipse/sy_Task.xml";
private DomXMLString(){}
public static void main(String[] args)throws Exception {
Document document = getTask(NETDATA_URL);
helloOK(document,LOCAL_PC_SAVEFILE_URL);
}
private static Document getTask(String netXMLDataURL) {
Document document = null;
DocumentBuilderFactory documentBF = DocumentBuilderFactory.newInstance();
documentBF.setNamespaceAware(true);
try{
DocumentBuilder documentB = documentBF.newDocumentBuilder();
InputStream inputStream = getSoapInputStream(netXMLDataURL);
document = documentB.parse(inputStream);
inputStream.close();
}catch(DOMException e){
e.printStackTrace();
return null;
}catch(ParserConfigurationException e){
e.printStackTrace();
return null;
}catch(SAXException e){
e.printStackTrace();
return null;
}catch(IOException e){
e.printStackTrace();
return null;
}
return document;
}
运行提示错误为:
java.io.IOException: Server returned HTTP response code: 500 for URL: http://183.131.242.70:8089/ship/interface/shipInfoService.asmx/getTask
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1626)
at org.tempuri.DomXMLString.getSoapInputStream(DomXMLString.java:79)
at org.tempuri.DomXMLString.getTask(DomXMLString.java:52)
at org.tempuri.DomXMLString.main(DomXMLString.java:39)
Exception in thread "main" java.lang.IllegalArgumentException: InputStream cannot be null
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:117)
at org.tempuri.DomXMLString.getTask(DomXMLString.java:53)
at org.tempuri.DomXMLString.main(DomXMLString.java:39)
之前也查了一些资料,想请问是不是我设置的路径有问题以及解决办法,求大神指点迷津
/**
*
* @param serviceUrl 服务名
* @param nameSpace
* @param methodName 执行的方法名
* @param paremateArrs 参数数据数组
* @param qNameArrs 变量数组
* @return
*/
public static Object CallSoapService(String serviceUrl ,String nameSpace,String methodName, Object[] paremateArrs,Object[] qNameArrs){
String endPoint = serviceUrl;
String actionUrl=nameSpace+methodName;
Object returnObj = null;
try{
Service service = new Service();
Call call = null;
call = (Call)service.createCall();
QName qName = new QName(nameSpace,methodName);
call.setOperationName(qName);
call.setSOAPActionURI(actionUrl);
for(int i=0,len=qNameArrs.length;i<len;i++){
call.addParameter(new QName(nameSpace,qNameArrs[i].toString()), org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
}
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
call.setTargetEndpointAddress(new URL(endPoint));
returnObj = call.invoke(paremateArrs);
}catch(Exception ie){
ie.printStackTrace();
}
System.out.println(returnObj);
return returnObj;
}
// 调用获得智慧社区的新增的用户信息
public static void main(String[] args) {
String url="http://221.226.86.35/webservice/UserOUService.asmx";
String method="GetFrameUserRecord";
String parameter="2016-09-05";
String namespace="http://tempuri.org/";
CallSoapService(url,namespace,method,new Object[]{parameter},new Object[]{"date"});
}
public static void main(String[] args) {
String url="http://172.22.173.21:8086/xinfang/services/xfinfoPhone";
//方法名
String method="queryByIdMydPj";
//参数
String parameter="{'xfid':'NJ2016010103390'}";
//命名空间
String namespace = "xfinfoPhoneService";
//最后一个参数随便填
CallSoapService(url,namespace,method,new Object[]{parameter},new Object[]{"asd"});
}
}
这有短源码你参考一下,好像需要引入xfire jar包。很久了 记得不是很清楚了
getTask 是你调用的方法名? 还有你调用webservice 的命名空间在吗?
"E:Myeclipse/sy_Task.xml"换成"E:\Myeclipse\sy_Task.xml"试试
不能直接读取网络上的文件需要用URLConnection或者httpclient 请求之后获取到这个流才行
URL url = new URL(NETDATA_URL);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
InputStream inputStream = conn.getInputStream();
http://www.blogjava.net/zjhiphop/archive/2009/04/29/webservice.html
服务端报500错误,说明你的访问地址不对: http://183.131.242.70:8089/ship/interface/shipInfoService.asmx/getTask,或者没有权限
你这不是调用WebService,是直接发HTTP请求下载XML了吧?服务器给你返回500内部错误了
相当于浏览器直接访问
http://183.131.242.70:8089/ship/interface/shipInfoService.asmx/getTask
试试
如果真的是通过SOAP交互,应该访问
http://183.131.242.70:8089/ship/interface/shipInfoService.asmx?wsdl
获得WEB服务描述(WSDL),然后使用相关技术进行访问,Java推荐CXF
相关技术一搜一大把
http://183.131.242.70:8089/ship/interface/shipInfoService.asmx?wsdl
检查你的webService接口URL地址以及参数是否正确。可以使用postman工具检测http接口以及参数传递的正确性。