C#中string 问题
代码如下,想输出+ GetstringIpAddress("string strIP"),也就是IP所查询的地址用的youdao. IP地址我在上面public string GetIP()已经获取,我想在下面的public static string GetstringIpAddress(string strIP)里写上一句strIP=GetIP()所获取的IP如何写
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Net;
using System.Net.Mail;
using System.IO;
using System.Xml;
public partial class _Default : System.Web.UI.Page
{
public SqlConnection conn;
protected void Page_Load(object sender, EventArgs e)
{
string sConnectionString = @"server=1-IO57X7TLRRVDX;database=shhr;user id=sa;password=111111;";
string sSql = "insert into IP(ip,DateTime,area) values('" + GetIP() + "',getdate(),'" + GetstringIpAddress("string strIP") + "')";
SqlConnection conn = new SqlConnection();
conn.ConnectionString = sConnectionString;
SqlCommand cmd = new SqlCommand();
cmd.CommandText = sSql;
cmd.Connection = conn;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
public string GetIP()
{
string result = String.Empty;
result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(result))
{
result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
if (string.IsNullOrEmpty(result))
{
result = HttpContext.Current.Request.UserHostAddress;
}
if (string.IsNullOrEmpty(result))
{
return "127.0.0.1";
}
return result;
}
public static string GetstringIpAddress(string strIP)//strIP为IP
{
string sURL = "http://www.youdao.com/smartresult-xml/search.s?type=ip&q="+strIP+"";//youdao的URL
string stringIpAddress = "";
using (XmlReader read = XmlReader.Create(sURL))//获取youdao返回的xml格式文件内容
{
while (read.Read())
{
switch (read.NodeType)
{
case XmlNodeType.Text://取xml格式文件当中的文本内容
if (string.Format("{0}", read.Value).ToString().Trim() != strIP)//youdao返回的xml格式文件内容一个是IP,另一个是IP地址,如果不是IP,那么就是IP地址
{
stringIpAddress=string.Format("{0}", read.Value).ToString().Trim();//赋值
}
break;
//other
}
}
}
return stringIpAddress;
}
}
[最优解释]
你把string result = String.Empty;定义成全局变量
public string GetIP()public static string GetstringIpAddress()//strIP为IP
{
result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(result))
{
result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
if (string.IsNullOrEmpty(result))
{
result = HttpContext.Current.Request.UserHostAddress;
}
if (string.IsNullOrEmpty(result))
{
return "127.0.0.1";
}
return result;
}
a a1=new a();
a1.GetIP();
public static string GetstringIpAddress()
{
string strIP= GetIP();//strIP
//……你自己的那些代码
}
为IP
直接把这里的string strIP改成上面public里 result 或者GetIP(),让他直接使用上面获取的IP地址,怎么改啊?
[其他解释]
求具体该法啊
[其他解释]
很复杂么,
在你的方法中把static去掉
然后在pageload中
string ip=?GetIP();
string sSql = string.Format("insert into IP(ip,DateTime,area) values('{0}',getdate(),'{1}')",ip ,GetstringIpAddress(ip));