[BlogEngine扩展]IP2Area

2008年12月5日  |  9:33 下午分类:Develop  |  标签:  |  

该插件将评论者的IP转为地区,数据取自于http://www.ip138.com
在这里表示感谢!
该扩展要在CommentAdded事件上进行处理,首先添加一个字段UserArea用来保存地区。
放心,这里的是异步处理的,不会造成页面的明显延迟。

代码如下:

/// <summary>
///Ip2Area 的摘要说明
/// </summary>
///
[Extension("IP转为地区", "1.0", "JasonYi")]
public class Ip2Area
{
public Ip2Area()
{
Post.CommentAdded += new EventHandler<EventArgs>(Post_CommentAdded);
}

void Post_CommentAdded(object sender, EventArgs e)
{
Comment p = sender as Comment;
if(p!=null)
new HttpAsyncGetIPArea(p,"http://www.ip138.com/ips.asp").Execute();
}
}
public class HttpAsyncGetIPArea
{
private Comment _comment;
private string _url;
public HttpAsyncGetIPArea(Comment c,string url)
{
_comment = c;
_url = url;
}
public Comment comment
{
get { return _comment; }
}
private HttpWebRequest Request;

public void Execute()
{
Request = (HttpWebRequest)WebRequest.Create(_url);
Request.Method = "POST";
Request.ContentType = "application/x-www-form-urlencoded";
Byte[] data = System.Text.Encoding.ASCII.GetBytes("action=2&ip=" + _comment.IP);
Request.ContentLength = data.Length;
using (Stream stream = Request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
Request.BeginGetResponse(ProcessRespose, this);
}
private void ProcessRespose(IAsyncResult async)
{
HttpAsyncGetIPArea item = (HttpAsyncGetIPArea)async.AsyncState;
try
{
using (HttpWebResponse response = (HttpWebResponse)item.Request.EndGetResponse(async))
{
string RStr = string.Empty;

using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default))
{

RStr = sr.ReadToEnd();
}
Regex reg = new Regex(@"本站主数据:([^<li>]+)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
MatchCollection mc = reg.Matches(RStr);
string area = string.Empty;
if (mc.Count > 0)
{
area = mc[0].Value.Replace("本站主数据:", string.Empty);
}
if (!string.IsNullOrEmpty(area))
{
string sql = "UPDATE be_PostComment SET UserArea=@Area WHERE PostCommentID=@Id";
SqlParameter[] para = { new SqlParameter("@Area", area), new SqlParameter("@Id", comment.Id) };
SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, System.Data.CommandType.Text, sql, para);
comment.UserArea = area;
}
}
}
catch (Exception ex)
{
//throw new System.Net.WebException("Get HttpResponse Error");
}
finally
{
Request.Abort();
}
}
}

转载时务必以超链接形式标明文章原始出处和作者信息。

12 位领导已批示 关于 “[BlogEngine扩展]IP2Area”

  1. 幻想曲 发表于: 十二月 5th, 2008 9:33 下午

    坐个沙发^_^

  2. LiuQiang 发表于: 十二月 5th, 2008 10:00 下午

    测试IP

  3. 幻想曲 发表于: 十二月 5th, 2008 10:02 下午

    ip138的数据库就是全啊:d

  4. Miles 发表于: 十二月 6th, 2008 12:49 下午

    这个不错

  5. 幻想曲 发表于: 十二月 6th, 2008 6:41 下午

    @Miles:
    ^_^^_^

  6. Shawer 发表于: 十二月 7th, 2008 11:23 上午

    Test.^_^

  7. Shawer 发表于: 十二月 7th, 2008 11:24 上午

    -__! :han: 天津市?十万八千里。:han:

  8. 幻想曲 发表于: 十二月 7th, 2008 3:47 下午

    @Shawer:
    不对么?
    http://www.baidu.com/s?wd=123.151.28.54

  9. Shawer 发表于: 十二月 8th, 2008 8:56 上午

    Test Again.-__!

  10. Shawer 发表于: 十二月 8th, 2008 8:56 上午

    这下对了。^_^

  11. Shawer 发表于: 十二月 8th, 2008 9:44 上午

    我想取纯真的IP应该会稍微准确一些。

  12. 幻想曲 发表于: 十二月 10th, 2008 7:19 下午

    @Shawer:
    嗯,可以考虑读取纯真数据库,这样可以不用去调httpwebrequest了。


发表您的评论

1410168172122013183195114157619