如何检查Web服务是否可访问?

时间:2011-08-10 06:40:02

标签: java web-services axis2

如何检查是否可以访问任何Web服务? 我可以看到服务列表,我也知道Web服务中存在的方法名称。但我不知道方法接受哪些参数。
以下是Web服务中提供的方法

 public OMElement getChildren(OMElement paramOMElement)
  {
     Object localObject2 = paramOMElement.toString();
     // Some other stuff 
  }

我试过http://machine_name/war_name/services/service_name/getChildren?a之类的东西  并得到以下错误

soapenv:Fault>
<faultcode>soapenv:Client</faultcode>
−
<faultstring>
Umarshaller error: Error during unmarshall <getChildren><a></a></getChildren>; nested exception is: 
    edu.harvard.i2b2.common.exception.I2B2Exception: Umarshaller error: Error during unmarshall <getChildren><a></a></getChildren>; nested exception is: 
    org.apache.axis2.AxisFault: Umarshaller error: Error during unmarshall <getChildren><a></a></getChildren>; nested exception is: 
    edu.harvard.i2b2.common.exception.I2B2Exception: Umarshaller error: Error during unmarshall <getChildren><a></a></getChildren>
</faultstring>
<detail/>
</soapenv:Fault>   

这个错误是否意味着我能够访问服务但发送错误的参数? 该服务也没有WSDL文件 如何检查服务是否可访问或如何找到所需的确切参数?

1 个答案:

答案 0 :(得分:3)

您需要向服务发送请求,看看是否有响应并且没有异常,从而确保服务器正在运行..  在java中编码很舒服,但这里是c#摘录:

function bool CheckIfServiceIsAlive(string url)
{
var isServiceUrlAlive= false;
            var req = WebRequest.Create(url);
            if (!string.IsNullOrEmpty(proxyServer))
            {
                var proxy = new WebProxy(proxyServer, 8080) { Credentials = req.Credentials };  //if you need to use a proxy
                WebRequest.DefaultWebProxy = proxy;
                req.Proxy = proxy;
            }
            else
            {
                req.Proxy =  new WebProxy();
            }
            try
            {
                var response = (HttpWebResponse)req.GetResponse();
                isServiceUrlAlive= true;
            }
            catch (WebException) { }

            return isServiceUrlAlive;

对于使用Apache Commons UrlValidator

的Java,可能有更简单的解决方案
UrlValidator urlValidator = new UrlValidator();
urlValidator.isValid("http://<your service url>");

或使用这样的方法获取响应代码

public static int getResponseCode(String urlString) throws MalformedURLException, IOException {
    URL u = new URL(urlString); 
    HttpURLConnection huc =  (HttpURLConnection)  u.openConnection(); 
    huc.setRequestMethod("GET"); 
    huc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)");
    huc.connect(); 
    return huc.getResponseCode();
}

或试试这个:http://www.java-tips.org/java-se-tips/java.net/check-if-a-page-exists-2.html

告诉我哪一个适合你..