我的代码在本地网络中工作,我知道它不能在本地网络之外工作,因为我使用Dns.GetHostEntry将IP地址解析为主机名,那很好......
但我的if语句不起作用,我收到错误"没有找到主机"尝试解析来自不同子网的主机名时出现错误消息。
我认为我的if语句会阻止此错误,但它不起作用,有人可以帮忙或告诉我如何解决它,代码如下......
public partial class _Default : System.Web.UI.Page
{
//public string IPaddr1 { get; private set; }
string serviceDesk = "eg@mail.co.uk";
string emailSubject = "MyPc";
string IPaddr = "";
string deviceName = "";
protected void Page_Load(object sender, EventArgs e)
{
getIP();
}
protected void getIP()
{
if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDER_FOR"] != null)
{
IPaddr = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDER_FOR"].ToString();
}
if (HttpContext.Current.Request.UserHostAddress.Length != 0)
{
IPaddr = HttpContext.Current.Request.UserHostAddress;
deviceName = Dns.GetHostEntry(IPAddress.Parse(IPaddr)).HostName;
}
if (HttpContext.Current.Request.UserHostAddress.Length != 0)
{
deviceName = "Device name could be found";
}
Label1.Text = "IP Address: " + " " + IPaddr;
Label2.Text = "PC Name: " + " " + deviceName;
}
private void EmailVerificationRequest(string recepientEmail, string subject)
{
try
{
using (MailMessage mailMessage = new MailMessage())
{
StringBuilder sbEmailBody = new StringBuilder();
sbEmailBody.Append("IP address " + IPaddr + "<br/>");
sbEmailBody.Append("Hostname " + " " + deviceName);
mailMessage.From = new MailAddress(ConfigurationManager.AppSettings["FromEmail"]);
mailMessage.Subject = subject;
mailMessage.Body = sbEmailBody.ToString();
mailMessage.IsBodyHtml = true;
mailMessage.To.Add(new MailAddress(recepientEmail));
SmtpClient smtp = new SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["Host"];
smtp.Port = Convert.ToInt32(ConfigurationManager.AppSettings["Port"]);
smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSsl"]);
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = ConfigurationManager.AppSettings["UserName"];
NetworkCred.Password = ConfigurationManager.AppSettings["Password"];
if (string.IsNullOrWhiteSpace(NetworkCred.UserName))
{
smtp.UseDefaultCredentials = true;
}
else
{
smtp.Credentials = NetworkCred;
smtp.UseDefaultCredentials = false;
}
smtp.Port = int.Parse(ConfigurationManager.AppSettings["Port"]);
try
{
smtp.Send(mailMessage);
}
catch (SmtpException e)
{
}
}
}
catch
{
}
}
protected void Button1_Click(object sender, EventArgs e)
{
EmailVerificationRequest(serviceDesk, emailSubject);
Response.Redirect("~/message.aspx");
}
}
答案 0 :(得分:1)
如果找不到主机名,则返回SocketException异常,其值为11001(Windows套接字错误WSAHOST_NOT_FOUND)。如果DNS服务器没有响应,则可以返回此异常。如果名称不是官方主机名或别名,或者在查询的数据库中找不到,则也可以返回此异常。
因此,我怀疑您的代码没有达到第三个if
条件,因为调用GetHostEntry()
会引发异常,您将其视为“错误”没有找到主机“消息”。
处理这个问题最直接的方法是使用try...catch
块来捕获特定的异常并处理它,例如:
try
{
deviceName = Dns.GetHostEntry(IPAddress.Parse(IPaddr)).HostName;
}
catch (SocketException)
{
deviceName = "Device name could be found";
}
这表示如果在调用SocketException
期间发生GetHostEntry()
,则代码将跳转到catch块,而不是停止应用程序的异常。
请注意,这假设任何SocketException
表示未找到IP地址,但这可能意味着DNS服务器无法联系或出现其他错误。
MSDN在异常处理和try...catch
阻止工作的方式上有很多。
希望这有帮助
答案 1 :(得分:0)
在从字典中获取值之前,必须检查字典是否包含您传递的密钥。你通过调用&#34; Contains&#34;来实现这一目标。方法
if (HttpContext.Current.Request.ServerVariables.Contains("HTTP_X_FORWARDER_FOR"))
{
// Your Code...
}
如果您直接检索字典的内容并且您要求的密钥不存在于字典对中,则字典不会返回null。相反,它会抛出KeyNotFoundException。