在我们的网站中,我们通过 API从Zendesk、Flickr、StopForumSpam中提取内容。当这些服务器关闭时,我们如何防止我们的网站被阻塞?
谢谢。
在我们的网站中,我们通过 API从Zendesk、Flickr、StopForumSpam中提取内容。当这些服务器关闭时,我们如何防止我们的网站被阻塞?
谢谢。
在您的 API 调用中实施合理的超时策略。您需要确保在加载时间比平时更长的内容和不可用的内容之间取得平衡。
If you're using MVC4/.NET 4.5+, you can use async and await to call the remote services, which will allow your application to process other operations while waiting for the network I/O to complete.
Edit: You'll still have to provide logic to manage errors/timeouts, but you won't be locking as many resources while waiting for a timeout to be triggered.
If you search around there's plenty of tutorials covering the use of async/await for network IO tasks.
Edit2: Valverij pointed out that (from the title of the question) you're obviously not able to use MVC4/.NET 4.5+, so you're going to need to use the much more verbose Asynchronous Controller approach to achieve the same effect.
这是迄今为止找到的最佳解决方案:
public static bool IsReady(string uri)
{
// Create a new 'HttpWebRequest' Object to the mentioned URL.
HttpWebRequest myHttpWebRequest = (HttpWebRequest) WebRequest.Create(uri);
// Set the 'Timeout' property of the HttpWebRequest to 1000 milliseconds.
myHttpWebRequest.Timeout = 1000;
HttpWebResponse myHttpWebResponse;
try
{
// A HttpWebResponse object is created and is GetResponse Property of the HttpWebRequest associated with it
myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
}
catch (Exception ex)
{
Debug.WriteLine("Error: " + ex.Message);
return false;
}
return myHttpWebResponse.StatusCode == HttpStatusCode.OK;
}