我在该用户中有一个文本框输入 URL,但是如果想在页面呈现时检查该 URL,那么该怎么办?
这是我的代码:
protected void btnRender_Click(object sender, EventArgs e)
{
string strResult = string.Empty;
WebResponse objResponse;
WebRequest objRequest = System.Net.HttpWebRequest.Create(urltxt.Text);
objResponse = objRequest.GetResponse();
using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
{
strResult = sr.ReadToEnd();
sr.Close();
}
strResult = strResult.Replace("<form id='form1' method='post' action=''>", "");
strResult = strResult.Replace("</form>", "");
TextBox1.Text = strResult.Trim();
div.InnerHtml = strResult.Trim();
}
我有这个代码来检查 URL 是否有效,所以你能告诉我在哪里调用它吗?{如果我还想检查 https 那么我该怎么做这个代码}
protected bool CheckUrlExists(string url)
{
// If the url does not contain Http. Add it.
// if i want to also check for https how can i do.this code is only for http not https
if (!url.Contains("http://"))
{
url = "http://" + url;
}
try
{
var request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "HEAD";
using (var response = (HttpWebResponse)request.GetResponse())
{
return response.StatusCode == HttpStatusCode.OK;
}
}
catch
{
return false;
}
}
文本框名称是 urltxt