asp.net 4.5 exercise~test6-12

webform1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="test6_12.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <strong>获取IP地址</strong><br />
        显示客户端的IP地址:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
        显示服务器的IP地址:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
        <asp:Button ID="Button1" runat="server" Text="获取" OnClick="Button1_Click" />
    </div>
    </form>
</body>
</html>

webform1.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace test6_12
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string ipClient = Request.ServerVariables.Get("Remote_Addr").ToString();
            TextBox1.Text = ipClient.ToString();

            string ipServer = Request.ServerVariables.Get("Local_Addr").ToString(); ;
            TextBox2.Text = ipServer.ToString();
        }
    }
}

 

Guess you like

Origin blog.csdn.net/modern358/article/details/114368953