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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7006809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:14:12  来源:igfitidea点击:

How to check if a web service is accessible?

javaweb-servicesaxis2

提问by xyz

How can I check if any web service is accessible ?
I can see the list of service also I know the method names present in the web service.But I don't know which parameters the method accepts.
Here is the method present in the web service

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

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

I tried something like http://machine_name/war_name/services/service_name/getChildren?aand got following error

我尝试了类似的操作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>   

Is this error mean I am able to access the service but sending wrong arguments ?
Also the service dont have WSDL file.
How can I check whether the service is accessible or how can I find out the exact parameters which are required ?

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

回答by Baz1nga

Well you need to send a request to the service and see if there is a response and there is no exception and thus make sure that the server is running.. Nt comfortable with coding in java but here is the c# excerpt:

好吧,您需要向服务发送请求并查看是否有响应并且没有异常,从而确保服务器正在运行。

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;

There might be easier solutions for java like using the Apache Commons UrlValidatorclass

可能有更简单的 Java 解决方案,例如使用Apache Commons UrlValidator

UrlValidator urlValidator = new UrlValidator();
urlValidator.isValid("http://<your service url>");

or use a method like this that gets the response code

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

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();
}

or try this: http://www.java-tips.org/java-se-tips/java.net/check-if-a-page-exists-2.html

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

Tell me which one worked for you..

告诉我哪一个对你有用..