in

ASP.NET Weblogs

This Blog

Syndication

ASP.NET Chinese Blogs

June 2005 - Posts

  • 如何从注册表读取文件的ContentType

    有的时候为了不让客户端知道文件的下载地址我们不直接公开文件的url,而是采用Response.WriteFile的形式输出文件,当我们需要直接在客户端的浏览器上打开这个文件的时候问题来了,如果我们不设定Response.ContentType的话,非文本文件在浏览器上直接将会直接输出得到的结果只能是乱码,但是ContentType非常多,如果采用switch-case的方法代码比较臃肿,而且还有可能遗留扩展名,我们打开注册表可以看到每一种文件在注册表中都能找到ContentType(可以按照value查找Content Type关键词看到),既然这样就能尝试从注册表读取ContentType了。下面是一个例子: using Microsoft.Win32; string filename = @" c:\11.doc " ; System.IO.FileInfo fi = new System.IO.FileInfo(filename); string fileextname = fi.Extension; string DEFAULT_CONTENT_TYPE = " application/unknown " ; RegistryKey regkey,fileextkey; string filecontenttype; try { regkey = Registry.ClassesRoot; fileextkey = regkey.OpenSubKey(fileextname); filecontenttype = fileextkey.GetValue( " Content Type " ,DEFAULT_CONTENT_TYPE).ToString(); } catch...
  • 如何向某网址Post信息,并得到CookieContainer以便以后直接通过验证(续)

    昨天有网友在msn问我如果有几个页面,给第一个页面post信息以后session设定一个值然后进入第二个页面,第二个页面调整了session后进入第三个页面,这样的情况怎么做,我写了一个例子,不知道是不是符合你的要求,如果不符合请留言。 static void Main( string [] args) { CookieContainer myCookieContainer = new CookieContainer(); Console.WriteLine(PostData( " http://localhost/csdn/step1.asp " , " step1=step1 " ,myCookieContainer)); Console.WriteLine(PostData( " http://localhost/csdn/step2.asp " , "" ,myCookieContainer)); Console.WriteLine(PostData( " http://localhost/csdn/step3.asp " , "" ,myCookieContainer)); } 写一个方法封装一下这个post的操作,第一个参数是地址,第二个参数是post的信息,第三个参数是CookieContainer,request的时候使用这个CookieContainer,在response的时候把CookieContainer赋值,因为本来CookieContainer就是引用类型的,所以可以不加ref或者out操作符。 public static string PostData( string url, string indata,CookieContainer myCookieContainer...
More Posts