这是我的代码:
/*
* This is a C# Program which displays the URL of an input IP address.
*/
using System;
using System.Net;
namespace CNT4704L
{
class MySocketLab
{
static void Main()
{
Console.Write("Enter an IP address (e.g., 131.247.2.211): ");
IPAddress addr = Console.ReadLine();
string strSiteName = Dns.GetHostEntry(addr);
Console.Write("\nHost name of ", addr);
Console.Write(" is ", strSiteName);
}
}
}
它说我不能隐式地将类型string
转换为System.Net.IPAddress
,但我不确定我想要获得的网址是什么,而不是字符串。
答案 0 :(得分:2)
你的问题就在这一行:
IPAddress addr = Console.ReadLine();
Console.ReadLine函数返回的字符串不是IPAddress。 只需将其更改为:
var addr = Console.ReadLine();