Start from Windows VistaIPv6 is enabled by default.In the environment where both IPv4 and IPv6 coexist, IPv6 has a high priority. Therefore, the obtained IP address is in the IPv6 format. Fortunately, to solve this problem, you do not have to deliberately disable IPv6. The same problem is the parsing of localhost in the host file. The IPv6 address is returned by default. For details, refer to this article.ArticleReturning an IPv4 address in an IPv6-Enabled environment, which also containsCode. The Code modified is as follows:
Using system;
Using system. net;
Using system. Web;
Public class ipnetworking
{
/// <Summary>
/// Obtain the IPv4 address of the guest host
/// </Summary>
/// <Returns> </returns>
Public static string getclientipv4 ()
{
String IPv4 = string. empty;
Foreach (IPaddress IP in DNS. gethostaddresses (getclientip ()))
{
If (IP. addressfamily. tostring () = "Internetwork ")
{
IPv4 = IP. tostring ();
Break;
}
}
If (IPv4! = String. Empty)
{
Return IPv4;
}
// The original code uses the DNS. gethostname method to retrieve data from the server rather than the client.
// Use the DNS. gethostentry method to retrieve DNS records from the obtained IPv6 address,
// Determine whether it belongs to the IPv4 protocol one by one. If it is an IPv4 address.
Foreach (IPaddress IP in DNS. gethostentry (getclientip (). Addresslist)
// Foreach (IPaddress IP in DNS. gethostaddresses (DNS. gethostname ()))
{
If (IP. addressfamily. tostring () = "Internetwork ")
{
IPv4 = IP. tostring ();
Break;
}
}
Return IPv4;
}
/// <Summary>
/// Obtain the address of the guest host
/// </Summary>
Public static string getclientip ()
{
If (null = httpcontext. Current. Request. servervariables ["http_via"])
{
Return httpcontext. Current. Request. servervariables ["remote_addr"];
}
Else
{
Return httpcontext. Current. Request. servervariables ["http_x_forwarded_for"];
}
}
}
Replacing IPv4 with IPv6 is a trend. Without large-scale deployment of IPv6, the above Code can be saved in an IPv4-dominated environment.
Reprinted from: http://www.cnblogs.com/shanyou/archive/2011/10/09/2204333.html
Start from Windows VistaIPv6 is enabled by default.In the environment where both IPv4 and IPv6 coexist, IPv6 has a high priority. Therefore, the obtained IP address is in the IPv6 format. Fortunately, to solve this problem, you do not have to deliberately disable IPv6. The same problem is the parsing of localhost in the host file. The IPv6 address is returned by default. The specific instructions can be seen in this article returning an IPv4 address in an IPv6-Enabled environment, the article also has code, run up does not meet expectations, the corrected code is as follows:
Using system;
Using system. net;
Using system. Web;
Public class ipnetworking
{
/// <Summary>
/// Obtain the IPv4 address of the guest host
/// </Summary>
/// <Returns> </returns>
Public static string getclientipv4 ()
{
String IPv4 = string. empty;
Foreach (IPaddress IP in DNS. gethostaddresses (getclientip ()))
{
If (IP. addressfamily. tostring () = "Internetwork ")
{
IPv4 = IP. tostring ();
Break;
}
}
If (IPv4! = String. Empty)
{
Return IPv4;
}
// The original code uses the DNS. gethostname method to retrieve data from the server rather than the client.
// Use the DNS. gethostentry method to retrieve DNS records from the obtained IPv6 address,
// Determine whether it belongs to the IPv4 protocol one by one. If it is an IPv4 address.
Foreach (IPaddress IP in DNS. gethostentry (getclientip (). Addresslist)
// Foreach (IPaddress IP in DNS. gethostaddresses (DNS. gethostname ()))
{
If (IP. addressfamily. tostring () = "Internetwork ")
{
IPv4 = IP. tostring ();
Break;
}
}
Return IPv4;
}
/// <Summary>
/// Obtain the address of the guest host
/// </Summary>
Public static string getclientip ()
{
If (null = httpcontext. Current. Request. servervariables ["http_via"])
{
Return httpcontext. Current. Request. servervariables ["remote_addr"];
}
Else
{
Return httpcontext. Current. Request. servervariables ["http_x_forwarded_for"];
}
}
}
Replacing IPv4 with IPv6 is a trend. Without large-scale deployment of IPv6, the above Code can be saved in an IPv4-dominated environment.