2015-02-09 95 views
0

我使用PHP开发货运应用程序,但它必须是WindowsForm应用程序。使用visual C#,我使用webBrowser显示网页,并且这些文件位于本地服务器上。 Xampp需要在应用程序运行之前启动所有服务,以便读取php等。这很好。 问题是。如果我不运行Xampp服务,浏览器将显示错误“导航到网页被取消”。原因很明显(没有服务器运行)。 所以我想在C#中做的是检查服务器是否正在运行或检查是否可用http://127.0.0.1/site。如果不是,那么显示一个错误messgaeBox,如果它可用,则不要做任何其他事情。 Message when Xampp is not runningC#检查Xampp服务器/本地主机是否运行

但我不希望员工看到此消息,所以如果xampp服务没有运行或本地主机不可访问我想显示一个消息框,而不是尝试加载页面。 Xampp Running with No Internet Connection. Works fine

+0

纠正我,如果我错了,但你有一个消息,说NO XAMPP的背景下,你是如何检测已经?没关系,我想我现在明白了。 – 2015-02-09 23:56:57

+0

对不起,我忘了说。这是粘贴屏幕截图后我在绘画中输入的文本。 “没有XAMPP”和“XAMPP正在运行”不在程序中。 – 2015-02-09 23:59:34

回答

0

使用HttpWebRequest尝试从您的网站拉一些页面,如果成功,您还可以检查HttpWebResponse上的StatusCode。应该是200或类似的。

1

您可能希望在.Net中制作Web请求并读取响应代码,然后使用条件逻辑来选择您的操作。

WebRequest request = WebRequest.Create ("http://127.0.0.1/site"); 
// If required by the server, set the credentials. 
request.Credentials = CredentialCache.DefaultCredentials; 
// Get the response. 
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
if(response.StatusCode == HttpStatusCode.NotFound) { 
    // show messagebox here 
} 

文档是相关WebRequest Class Documentation

我的代码是未经测试,但它应该做你所需要的。

+0

这么多错误。到底如何使用WebRequest类和HttpWebResponse? – 2015-02-10 03:36:54

+0

嘿。将代码与另一个代码混合后,我就可以开始工作了!谢谢。我会将其标记为答案。 – 2015-02-10 05:18:21

1

在丹尼尔巷的帮助下,我得到了它的工作。

using System.IO; 
using System.Web; 
using System.Net; 
WebRequest request = WebRequest.Create("http://127.0.0.1/site"); 
      try 
      { 
       using (WebResponse response = request.GetResponse()) 
       { 
        //Load Webpage 
       } 
      } 
      catch (WebException erf) 
      { 
       using (WebResponse response = erf.Response) 
       { 
        var errorForm = new error(); 
        errorForm.Show(); 
        this.Close(); 
       } 
      } 

从“WebRequest ...”处于事件(加载)中。

下面是结果: XAMPP Running So Form Opens


XAMPP Not Running So Error Form is shown instead of Form

+0

我很高兴你的工作! – 2015-02-10 08:26:04

相关问题