使用路径检查我的本地 ip 是否有效
Check my local ip with a path whether it is valid or not
你好,我尝试检查我的 ip(在本地服务器上)是否有效并为其添加路径,但问题是当我测试“192.168.0.235”时它不被视为 ip 地址/cmd.html?c-31" 它很好地显示在我的浏览器上。这是代码:
public static bool check_ip(string ip)
{
//192.168.0.235/cmd.html?c=31
string path_ip = ip + "/cmd.html?c=31"; // error not an ip
Ping ping = new Ping();
IPAddress address = IPAddress.Parse("path_ip");
PingReply pong = ping.Send(address, 100);
if (pong.Status == IPStatus.Success)
{
return true;
}
return false;
}
如何让我的函数检查带有路径的 ip(例如:“192.168.0.235/cmd.html?c-31”)?
您应该只检查 ip 部分:
public static bool check_ip(string ip)
{
//192.168.0.235/cmd.html?c=31
string path_ip = ip + "/cmd.html?c=31"; // error not an ip
Ping ping = new Ping();
IPAddress address = IPAddress.Parse(ip);
PingReply pong = ping.Send(address, 100);
if (pong.Status == IPStatus.Success)
{
return true;
}
return false;
}
当心这样的行:
// "path_ip" is not the value of your variable, but a string value "path_ip"
IPAddress address = IPAddress.Parse("path_ip");
如果您想要将 url 当作浏览器来检查:
using var client = new HttpClient();
var result = await client.GetAsync("http://192.168.0.235/cmd.html?c=31");
Console.WriteLine(result.StatusCode); // if the status code is in the 2XX range, you are ok.
你好,我尝试检查我的 ip(在本地服务器上)是否有效并为其添加路径,但问题是当我测试“192.168.0.235”时它不被视为 ip 地址/cmd.html?c-31" 它很好地显示在我的浏览器上。这是代码:
public static bool check_ip(string ip)
{
//192.168.0.235/cmd.html?c=31
string path_ip = ip + "/cmd.html?c=31"; // error not an ip
Ping ping = new Ping();
IPAddress address = IPAddress.Parse("path_ip");
PingReply pong = ping.Send(address, 100);
if (pong.Status == IPStatus.Success)
{
return true;
}
return false;
}
如何让我的函数检查带有路径的 ip(例如:“192.168.0.235/cmd.html?c-31”)?
您应该只检查 ip 部分:
public static bool check_ip(string ip)
{
//192.168.0.235/cmd.html?c=31
string path_ip = ip + "/cmd.html?c=31"; // error not an ip
Ping ping = new Ping();
IPAddress address = IPAddress.Parse(ip);
PingReply pong = ping.Send(address, 100);
if (pong.Status == IPStatus.Success)
{
return true;
}
return false;
}
当心这样的行:
// "path_ip" is not the value of your variable, but a string value "path_ip"
IPAddress address = IPAddress.Parse("path_ip");
如果您想要将 url 当作浏览器来检查:
using var client = new HttpClient();
var result = await client.GetAsync("http://192.168.0.235/cmd.html?c=31");
Console.WriteLine(result.StatusCode); // if the status code is in the 2XX range, you are ok.