java 如何检查Web服务是否可用,然后一口气连接到它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12920054/
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
How to check if web service is available, and then connect to it in one go?
提问by Kalina
I want to check if a web service is available before connecting to it, so that if it is not available, I can display a dialog that says so. My first attempt is this:
我想在连接到某个 Web 服务之前检查它是否可用,如果它不可用,我可以显示一个对话框来说明。我的第一次尝试是这样的:
public void isAvailable(){
// first check if there is a WiFi/data connection available... then:
URL url = new URL("URL HERE");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Connection", "close");
connection.setConnectTimeout(10000); // Timeout 10 seconds
connection.connect();
// If the web service is available
if (connection.getResponseCode() == 200) {
return true;
}
else return false;
}
And then in a separate class I do
然后在一个单独的班级我做
if(...isAvailable()){
HttpPost httpPost = new HttpPost("SAME URL HERE");
StringEntity postEntity = new StringEntity(SOAPRequest, HTTP.UTF_8);
postEntity.setContentType("text/xml");
httpPost.setHeader("Content-Type", "application/soap+xml;charset=UTF-8");
httpPost.setEntity(postEntity);
// Get the response
HttpClient httpclient = new DefaultHttpClient();
BasicHttpResponse httpResponse = (BasicHttpResponse) httpclient.execute(httpPost);
// Convert HttpResponse to InputStream for parsing
HttpEntity responseEntity = httpResponse.getEntity();
InputStream soapResponse = responseEntity.getContent();
// Parse the result and do stuff with the data...
}
However I'm connecting to the same URL twice, and this is inefficient and probably slowing down my code.
但是,我连接到同一个 URL 两次,这是低效的,可能会减慢我的代码速度。
First of all, is it?
首先,是吗?
Secondly, what's a better way to do this?
其次,有什么更好的方法可以做到这一点?
采纳答案by dardo
I would just try connecting, if it times out, handle the exception, and display an error.
我只是尝试连接,如果超时,处理异常并显示错误。
回答by Jigish Chawda
May be you should consider looking at HttpConnectionParams class methods.
可能您应该考虑查看 HttpConnectionParams 类方法。
public static void setConnectionTimeout (HttpParams params, int timeout)
Sets the timeout until a connection is etablished. A value of zero means the timeout is not used. The default value is zero.
public static void setSoTimeout (HttpParams params, int timeout)
Sets the default socket timeout (SO_TIMEOUT) in milliseconds which is the timeout for waiting for data. A timeout value of zero is interpreted as an infinite timeout. This value is used when no socket timeout is set in the method parameters.
public static void setConnectionTimeout (HttpParams params, int timeout)
设置超时,直到建立连接。零值表示不使用超时。默认值为零。
public static void setSoTimeout (HttpParams params, int timeout)
以毫秒为单位设置默认套接字超时(SO_TIMEOUT),这是等待数据的超时时间。超时值为零被解释为无限超时。当方法参数中未设置套接字超时时,将使用此值。
Hope this helps.
希望这可以帮助。
回答by Marcin S.
As above and here is how you check for times out:
如上所述,这里是您检查超时的方法:
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
// The default value is zero, that means the timeout is not used.
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 5000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
And then pass the HttpParameters to a constructor of httpClient:
然后将 HttpParameters 传递给 httpClient 的构造函数:
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);