简体   繁体   English

如何在Java中模拟网站?

[英]How to simulate a site down in Java?

My app accesses info on the web, during my development how do I simulate a site is down in Java ? 我的应用程序访问网络上的信息,在我的开发过程中如何模拟Java中的网站? If the url gets me the page my app processes it, but if a site for some reason is temporarily down, or even if the site is up, but the url returns invalid info or it could be the Internet connection is disabled, how can a Java app distinguish those situations ? 如果网址获取我的应用程序处理它的页面,但如果某个网站由于某种原因暂时关闭,或者即使网站已启动,但网址返回无效信息或者可能是因特网连接被禁用,那么如何Java应用程序区分这些情况? And be able to tell which is occurring ? 并能够分辨出哪个正在发生?

Edit : I'm trying to write my Java app so that when it encounters different url accesses, it knows which case it is dealing with and tell user accordingly. 编辑:我正在尝试编写我的Java应用程序,以便在遇到不同的url访问时,它知道它处理的是哪种情况并相应地告诉用户。

The connect() method from the java.net.URLConnection class throws a SocketTimeoutException if it cannot connect to the given URL. 如果java.net.URLConnection类中connect()方法无法连接到给定的URL,则抛出SocketTimeoutException You can set a timeout via the setConnectTimeout(int) method and react on the SocketTimeoutException exception which is thrown in this case. 您可以通过setConnectTimeout(int)方法设置超时, setConnectTimeout(int)在这种情况下引发的SocketTimeoutException异常做出反应。

For the purposes of testing or simulating different error conditions, here are a few additional suggestions: 出于测试或模拟不同错误条件的目的,以下是一些其他建议:

  • Ensure that the URL's domain is unreachable (domain resolves to an ip address, but the ip address is not reachable). 确保URL的域不可达(域解析为IP地址,但无法访问IP地址)。 You can fake this by adding an entry in your /etc/hosts file (if unix like) 你可以通过在/ etc / hosts文件中添加一个条目来假装这个(如果像unix一样)
  • Ensure that the URL's domain is not resolvable, ie a fake domain address that does not exist 确保URL的域不可解析,即不存在的虚假域地址
  • Unplug your client's network cable/wireless: what happens? 拔掉客户端的网线/无线:会发生什么?
  • Unplug your client's network cable while in the middle of a transfer: what happens? 在转移过程中拔掉客户的网线:会发生什么?
  • Unplug your test server's network cable in the middle of a transfer? 在转移过程中拔掉测试服务器的网线? What does the client see? 客户看到了什么?
  • kill the test server's web server while in the middle of a transfer? 在转移过程中杀死测试服务器的Web服务器? What happens to the client's URL connection? 客户端的URL连接会发生什么变化?
  • Shutdown the test server's web server and what does the client see when it tries to connect? 关闭测试服务器的Web服务器以及客户端在尝试连接时看到了什么?

As you test these different scenarios you will be surprised to learn what happens to your client. 当您测试这些不同的场景时,您会惊讶地发现客户端会发生什么。 If you are lucky you will get exceptions. 如果你很幸运,你会得到例外。 Sometimes the client may hang forever. 有时客户可能永远挂起。 But that's the real world. 但那是现实世界。 Good luck. 祝好运。

If a site is down it will a 404 code and it will throw a java.net.UnknownHostException . 如果一个站点关闭,它将是一个404代码,它将抛出一个java.net.UnknownHostException You will need to catch it or catch as IOException . 您将需要捕获它或捕获IOException For other status codes you can read them using the following code snippet. 对于其他状态代码,您可以使用以下代码段阅读它们。 You can see a list of HTTP status codes here. 您可以在此处查看HTTP状态代码列表。

You can read the response code this way. 您可以通过这种方式阅读响应代码。

   URL url = new URL ("http://someurl.doesnotexist.com");
   URLConnection connection = url.openConnection();
   connection.connect();
   HttpURLConnection httpConnection = (HttpURLConnection) connection; 
   int code = httpConnection.getResponseCode();
   //do something with the code

The HTTP response codes are they key. HTTP响应代码是关键。 Internal server error (500) or Page not found (404) can be used to distinguish those situations (probably in combination with additional status codes). 可以使用内部服务器错误(500)或找不到页面(404)来区分这些情况(可能与其他状态代码组合)。

If you're using spring, you can use an Interceptor to generate different responses at different times (so you can simulate that the site is down). 如果您正在使用spring,则可以使用Interceptor在不同时间生成不同的响应(这样您就可以模拟站点已关闭)。 A nice example is provided in the documentation. 文档中提供了一个很好的示例。 http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-handlermapping-interceptor http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-handlermapping-interceptor

If you need to deny access to a specific Internet address, you can use a firewall application. 如果您需要拒绝访问特定的Internet地址,则可以使用防火墙应用程序。 Or, even simpler, if the IP address of your server is resolved from a domain name, you can point that domain name to your loopback address (127.0.0.1) using the /etc/hosts file (%SystemRoot%\\system32\\drivers\\etc\\ for Windows). 或者,更简单的是,如果从域名解析服务器的IP地址,则可以使用/ etc / hosts文件将该域名指向您的环回地址(127.0.0.1)(%SystemRoot%\\ system32 \\ drivers \\等等\\为Windows)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM