1.winform设置2个文本框,一个按钮事件。
2.后台代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
namespace WindowsFormsApplication7
{
public partial class Form1 : Form
{
bool isOk = false;
public Form1()
{
InitializeComponent();
}
public static bool IsWebServiceAvailable(string webServiceUrl)
{
try
{
WebRequest webRequest = WebRequest.Create(webServiceUrl);
HttpWebRequest httpRequest = webRequest as HttpWebRequest;
httpRequest.Timeout = 3000;
if (httpRequest == null)
return false;
try
{
HttpWebResponse resp = (HttpWebResponse)httpRequest.GetResponse();
return true;
}
catch (Exception ex)
{
MessageBox.Show(" " + ex.Message, "ERROR");
return false;
}
}
catch (Exception ex)
{
MessageBox.Show(" " + ex.Message, "ERROR");
return false;
}
}
private void button1_Click(object sender, EventArgs e)
{
isOk = IsWebServiceAvailable(textBox1.Text);
textBox2.Text = isOk.ToString();
}
}
}
3.执行效果