C#使用正则判断IP地址

C#使用正则判断一个字符串是否为IP地址:

private boolean isIp(String ip) {
    String rex =
            "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
                    "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
                    "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
                    "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
    Pattern pattern = Pattern.compile(rex);
    Matcher matcher = pattern.matcher(ip);
    return matcher.find();
}

ASP.NET MVC缓存absoluteExpiration、slidingExpiration两个参数的疑惑

对C#缓存absoluteExpiration、slidingExpiration两个参数有点疑惑,看了很多资料终于搞明白 cache 中 absoluteExpiration,slidingExpiration 这两个参数的含义。

absoluteExpiration:用于设置绝对过期时间,它表示只要时间一到就过期,所以类型为System.DateTime,当给这个参数设置了一个时间时,slidingExpiration参数的值就只能为Cache.NoSlidingExpiration,否则出错;

slidingExpiration:用于设置可调过期时间,它表示当离最后访问超过某个时间段后就过期,所以类型为System.TimeSpan,当给这个参数设置了一个时间段时,absoluteExpiration的值就只能为Cache.NoAbsoluteExpiration,否则出错;

VS2017在使用VS2015编译的libcurl静态库时报错

VS2017在使用VS2015编译的libcurl静态库时报错,错误信息如下:

1>libcurld.lib(cookie.obj) : error LNK2019: unresolved external symbol ___iob_func referenced in function _Curl_cookie_init

1>libcurld.lib(url.obj) : error LNK2001: unresolved external symbol ___iob_func

1>libcurld.lib(mprintf.obj) : error LNK2001: unresolved external symbol ___iob_func

1>libcurld.lib(formdata.obj) : error LNK2001: unresolved external symbol ___iob_func

1> fatal error LNK1120: 1 unresolved externals


解决方案:

在stdafx.h中增加如下 代码:

FILE _iob[] = { *stdin, *stdout, *stderr };
extern "C" FILE * __cdecl __iob_func(void) { return _iob; }

再重新编译即可通过。

sql server游标

sql server游标的使用示例代码:

declare test_cursor cursor for
select  t1.baijiahao as bjh_name, t1.baijiahao_home_url as home_url, t2.category_name as domain from baijia_articles t1
inner join category t2
on t1.cat_id = t2.id
open test_cursor
declare @bjh_name nvarchar(50)
declare @home_url nvarchar(90)
declare @domain nvarchar(50)
fetch next from test_cursor into @bjh_name, @home_url, @domain
while @@fetch_status=0
begin
       
       if not exists ( select id from baijiahao where bjh_name=@bjh_name)  insert into baijiahao(bjh_name, home_url, domain) values(@bjh_name, @home_url, @domain)
       fetch next from test_cursor into @bjh_name, @home_url, @domain
end
close test_cursor
deallocate test_cursor

SQL SERVER查询占用CPU高的SQL语句

SQL SERVER查询占用CPU高的SQL语句:

SELECT TOP 10
   total_worker_time/execution_count AS avg_cpu_cost, plan_handle,
   execution_count,
   (SELECT SUBSTRING(text, statement_start_offset/2 + 1,
      (CASE WHEN statement_end_offset = -1
         THEN LEN(CONVERT(nvarchar(max), text)) * 2
         ELSE statement_end_offset
      END - statement_start_offset)/2)
   FROM sys.dm_exec_sql_text(sql_handle)) AS query_text
FROM sys.dm_exec_query_stats
ORDER BY [avg_cpu_cost] DESC

C#使用phantomjs采集页面HTML

C#使用phantomjs采集页面HTML。

code.js

phantom.outputEncoding="gb2312"
system = require('system')   
address = system.args[1];//获得命令行第二个参数 接下来会用到   
//console.log('Loading a web page');   
var page = require('webpage').create();   
var url = address;   
//console.log(url);   
page.open(url, function (status) {   
    //Page is loaded!   
    if (status !== 'success') {   
        console.log('Unable to post!');   
    } else {   
        //console.log(page.content);   
        //var title = page.evaluate(function() {   
        //  return document.title;//示范下如何使用页面的jsapi去操作页面的  
        //  });   
        //console.log(title);   
        //console.log(encodeURIComponent(page.content));   
        console.log(page.content);   
    }     
    phantom.exit();   
});

C#代码

public string getAjaxCotnent(String url)
{
    var b = System.Reflection.Assembly.GetEntryAssembly().Location;
    var path = Path.GetDirectoryName(b); // 末尾不带斜杠

    ProcessStartInfo start = new ProcessStartInfo(path + "//phantomjs.exe");

    start.Arguments = "code.js" + " " + url;//设置命令参数
    StringBuilder sb = new StringBuilder();
    start.CreateNoWindow = false;//不显示dos命令行窗口
    start.RedirectStandardOutput = true;//
    start.RedirectStandardInput = true;//
    start.UseShellExecute = false;//是否指定操作系统外壳进程启动程序
    Process p = Process.Start(start);

   // string encoding = p.StandardOutput.CurrentEncoding.ToString();
    StreamReader reader = p.StandardOutput;//截取输出流
    string line = reader.ReadLine();//每次读取一行
    sb.AppendLine(line);
    while (!reader.EndOfStream)
    {
        line = reader.ReadLine();
        sb.AppendLine(line);
    }
    p.WaitForExit();//等待程序执行完退出进程
    p.Close();//关闭进程
    reader.Close();//关闭流
    string strRet = System.Web.HttpUtility.UrlDecode(sb.ToString());
    return strRet;
}

调用

var url = "http://po.baidu.com/feed/share?context=%7B%22nid%22%3A%22news_3493960622142783493%22%2C%22sourceFrom%22%3A%22bjh%22%7D";
var detailHtml = getAjaxCotnent(url);