1
我想生成特定鏈接的所有錯誤,如果鏈接不工作,那麼它應該顯示特定的Web服務器錯誤。如何生成所有的Web服務器錯誤programaticallly
這是我的代碼。請建議應該在哪裏程序應該做,我可以得到所有的錯誤,如果鏈接不上的「如何使用Visual Studio和MSDN上找到方法和相關的例外幫助」工作
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
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>", "");
//strResult = strResult.Replace("<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" /><html xmlns="http://www.w3.org/1999/xhtml">");
div.InnerHtml = strResult;
}
protected void btn_createlink_Click(object sender, EventArgs e)
{
var links = TextBox1.Text.Split(new string[] { "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries);
foreach (var link in links)
{
if (!IsLinkWorking(link))
{
//Here you can show the error. You don't specify how you want to show it.
TextBox2.Text += string.Format("{0}\nNot working\n\n ", link);
}
else
{
TextBox2.Text += string.Format("{0}\n working\n\n", link);
}
}
}
bool IsLinkWorking(string url)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
//You can set some parameters in the "request" object...
request.AllowAutoRedirect = true;
ServicePointManager.ServerCertificateValidationCallback = (s, cert, chain, ssl) => true;
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
return true;
}
catch
{
//TODO: Check for the right exception here
return false;
}
}
請添加上你有什麼問題的代碼信息。 – 2013-03-05 05:51:45
如果(!!IsLinkWorking(link)) { TextBox2.Text + = string.Format(「{0} \ nNot working \ n \ n」,link),我想生成類似401錯誤的錯誤代碼。 } else { TextBox2.Text + = string.Format(「{0} \ n working \ n \ n」,link); }} – hitarth 2013-03-05 05:53:21
@AlexeiLevenkov我需要打印特定的錯誤,而不是沒有工作的打印 – hitarth 2013-03-05 06:00:25