惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
Simon Willison's Weblog
Simon Willison's Weblog
Microsoft Security Blog
Microsoft Security Blog
Y
Y Combinator Blog
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
F
Fortinet All Blogs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
A
About on SuperTechFans
Last Week in AI
Last Week in AI
月光博客
月光博客
有赞技术团队
有赞技术团队
P
Proofpoint News Feed
MyScale Blog
MyScale Blog
Martin Fowler
Martin Fowler
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
Check Point Blog
U
Unit 42
The Register - Security
The Register - Security
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Hugging Face - Blog
Hugging Face - Blog
阮一峰的网络日志
阮一峰的网络日志
V
Visual Studio Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
D
DataBreaches.Net
WordPress大学
WordPress大学
aimingoo的专栏
aimingoo的专栏
H
Hacker News: Front Page
Recent Announcements
Recent Announcements
C
CXSECURITY Database RSS Feed - CXSecurity.com
Latest news
Latest news
小众软件
小众软件
P
Palo Alto Networks Blog
PCI Perspectives
PCI Perspectives
Security Latest
Security Latest
S
Secure Thoughts
Scott Helme
Scott Helme
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Threat Research - Cisco Blogs
P
Proofpoint News Feed
M
MIT News - Artificial intelligence
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Google DeepMind News
Google DeepMind News
Recorded Future
Recorded Future
O
OpenAI News
S
Securelist
云风的 BLOG
云风的 BLOG
H
Help Net Security
T
Troy Hunt's Blog

博客园 - 网际飞狐

异步服务框架 如何在TFS中用命令行提交更新 CollabNet Subversion 输出自定义日期格式 - 网际飞狐 - 博客园 你正确关闭WCF链接了吗? 通过OperationContext添加消息头信息 PHP在II7安装指南 [Google App Engine] Hello, world! 在IIS7中配置使用Python 2008年12月小记(NewSequentialID(),ADO.NET Data Service,Visual Studio Tips,安装Django,JQuery智能感知) [OpenAPI] html标签分析 System.Web.Routing 使用基础 JS通过服务代理调用跨域服务 对硬编码WCF服务的封装(提供服务和客户端调用的封装,调用样例....) Observer Pattern, Delegate and Event How to view the W3WP process by c#? 项目框架概要 2008年10月小记(SQL删除重复记录,生成表结构,字符串特性,statistics io) WinDbg使用摘要
Notes for 2008-11(GetRange, backup,file hash, PostRequest, QueryString)
网际飞狐 · 2008-11-10 · via 博客园 - 网际飞狐

1、List<T>泛型中并没有实现ICloneable接口,也就是没有实出Clone方法,所以我们在克隆一个List时可以使用GetRange方法来代替它。

List<int> oldList = new List();
oldList.Add(
1);
oldList.Add(
2);
List
<int> newList = oldList.GetRange(0,oldList.Count);

2、 backup数据库

BACKUP DATABASE ProfileDB TO DISK = 'D:\FULL_DATA\ProfileDB.bak' 
WITH
 checksum, INIT, NOUNLOAD, NAME=N'ProfileDB Full backup', SKIP, STATS = 10, NOFORMAT

3、计算文件的Hash值:

    protected void Page_Load(object sender, EventArgs e)
    {
        
string filePath = Request.PhysicalPath;
        
string hash = string.Empty;
        
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
        {
            
using (MD5 md5 = MD5.Create())
            {
                
byte[] hashData = md5.ComputeHash(fs);
                hash 
= ConvertToHexString(hashData);
                md5.Clear();
            }
            fs.Close();
        }
        Response.Write(hash);
    }
    
private static string ConvertToHexString(byte[] bytes)
    {
        
int length = bytes.Length;
        StringBuilder sb 
= new StringBuilder();
        
foreach (byte data in bytes)
        {
            sb.Append(data.ToString(
"x2"));
        }
        
return sb.ToString();
    }

hash结果都是以32个字符来表示的16进制串,通过对每位招行ToString("x2")实现。

4、封装对某一页面Post和Get操作。

        /// <summary>
        
/// 通过Post和Get方式请求页面
        
/// </summary>
        
/// <param name="url">需要Post到的页面url</param>
        
/// <param name="postData">post数据的字典</param>
        
/// <param name="queryData">get查询的字典</param>
        
/// <param name="timeout">超时时间(毫秒)</param>
        
/// <returns>请求后的返回</returns>
        public static string RequestUrl(string url, IDictionary<stringstring> postData, IDictionary<stringstring> queryData, int timeout)
        {
            
//参数验证
            if (url.IndexOfAny(new char[] { '?''&' }) > -1)
                
throw new ArgumentException("url不应该带参数");
            
if (!url.StartsWith("http://"))
                
throw new ArgumentException("url必须以\"http://\"开头");
            if (postData == null)
                postData 
= new Dictionary<stringstring>();
            
if (queryData == null)
                queryData 
= new Dictionary<stringstring>();//构造url
            StringBuilder urlBuilder = new StringBuilder();
            
foreach (var kv in queryData)
            {
                urlBuilder.Append(
string.Format(CultureInfo.InvariantCulture, "{0}={1}&", kv.Key, HttpUtility.UrlEncode(kv.Value)));
            }
            
string suffix = urlBuilder.Length > 0 ? ("?" + urlBuilder.ToString().TrimEnd('&')) : "";
            
string postUrl = url + suffix;//构造Post数据
            StringBuilder dataBuilder = new StringBuilder();
            
foreach (var kv in postData)
            {
                dataBuilder.Append(
string.Format(CultureInfo.InvariantCulture, "{0}={1}&", kv.Key, kv.Value));
            }
            
string data = dataBuilder.Length > 0 ? dataBuilder.ToString().TrimEnd('&') : "";
            
byte[] dataBytes = Encoding.UTF8.GetBytes(data);//发出请求
            string content = string.Empty;//HttpWebRequest
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(postUrl);
            request.Method 
= "POST";
            request.ContentType 
= "application/x-www-form-urlencoded";
            request.ContentLength 
= dataBytes.Length;
            request.Timeout 
= timeout;try
            {
                
//把dataBytes写入HttpWebRequest
                using (Stream requestStream = request.GetRequestStream())
                {
                    requestStream.Write(dataBytes, 
0, dataBytes.Length);
                    requestStream.Close();
                }
                
//获取HttpWebResponse
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    
if (response.StatusCode == HttpStatusCode.OK)
                    {
                        
using (Stream stream = response.GetResponseStream())
                        {
                            
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                            {
                                content 
= reader.ReadToEnd();
                                reader.Close();
                            }
                            stream.Close();
                        }
                    }
                    response.Close();
                }
            }
            
catch (System.Net.WebException) { }
            
finally
            {
                
if (request != null)
                {
                    request.Abort();
                    request 
= null;
                }
            }
return content;
        }

5、Request.QueryString里对查询字符串强制进行了一次UrlDecode,但如何使用Request["id"]方式获取查询值时则需要手工进行一次UrlDecode