[C#]使用 HttpWebRequest 瀏覽整合式 windows 認證環境的網站

[C#]使用 HttpWebRequest 瀏覽整合式 windows 認證環境的網站 – Incredible C Sharp- 點部落

先來看一下程式碼,標準 MSDN 的寫法:

01 void MailSenderTrigger(string btNo)
02 {
03 try
04 {
05 string URL = “http://www.urWebName.com/"; //欲瀏覽的特定頁面
06
07 //初始化 HttpWebRequest
08 HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(URL);
09
10 // 設定內容類型
11 myHttpWebRequest.ContentType = “application/x-www-form-urlencoded";
12
13 //初始化 HttpWebResponse
14 HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
15 Stream msgstream = myHttpWebResponse.GetResponseStream(); //接收回傳訊息
16
17 Encoding encode = System.Text.Encoding.GetEncoding(“utf-8″); //設定編碼方式
18
19 //用 StreamReader 讀出回傳訊息
20 StreamReader readStream = new StreamReader(msgstream, encode);
21
22 string Syslog = “";
23 Syslog = readStream.ReadToEnd();
24 readStream.Close();
25 }
26 catch (Exception e3)
27 {
28 Response.Write(e3.ToString());
29 }
30 }

爬了一下文之後,找到好幾種解決的方法,
例如將頁面設定為可匿名存取(可以過,但是一般不建議這樣的方式),
或是在 web.config 中加入:(試過還是不行)
1

最後找到的解法是在 GetResponse() 之前加上這一段:
1 myHttpWebRequest.Credentials = CredentialCache.DefaultCredentials;

這樣就可以用當時使用者當時登錄的身分進行認證,
另外,DefaultCredentials 只記錄了程式運作時的一些認證訊息,
而不會紀錄使用者的帳號和密碼,所以對安全性來說有一定幫助,
如果硬要檢視詳細的內容,也只會得到一堆空字串,
這或許也是微軟的保護機制吧!?

如果你要指定一個身分去登錄網站的話,可以這樣做:
1 myHttpWebRequest.PreAuthenticate = true;
2 NetworkCredential netCreden=new NetworkCredential(“userName", “userPW"); //登入的帳號密碼
3 myHttpWebRequest.Credentials = netCreden.GetCredential(new Uri(URL), “authType"); //要瀏覽的網址以及認證類型
4

當然,如果你的認證方式是預設值且網址URL跟最前面相同的話,
最後一行可以簡寫為
1 myHttpWebRequest.Credentials=netCreden;

[C#] 利用HttpWebRequest及HttpWebRequest製作網路爬蟲

[C#] 利用HttpWebRequest及HttpWebRequest製作網路爬蟲 @ 大海男兒要出征 :: 痞客邦 PIXNET ::

[C#] 利用HttpWebRequest及HttpWebRequest製作網路爬蟲

//準備data
CookieContainer cookieContainer = new CookieContainer();
string URI = “https://myinfo.tcu.edu.tw/login_check.asp";
string referer = “https://myinfo.tcu.edu.tw/login.asp";
string postString = “username=user&password=passw@rd";
byte[] postData = Encoding.ASCII.GetBytes(postString);
 
//設定相關參數
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URI);
request.Method = “POST";
request.AllowAutoRedirect = false;
//偽裝
request.Referer = referer;
request.ContentType = “application/x-www-form-urlencoded";
request.CookieContainer = cookieContainer;
request.ContentLength = postData.Length;
 
//送出httprequest資料流
Stream outputStream = request.GetRequestStream();
outputStream.Write(postData, 0, postData.Length);
outputStream.Close();
 
//取得httpresponse資料流
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding(“big5″));
string srcString = reader.ReadToEnd();
 
//輸出header資訊
Response.Write(response.Headers[HttpResponseHeader.Location]);
 
//輸出內文
Response.Write(srcString);

Smart Thread Pool

Smart Thread Pool – Home

Project Description
Smart Thread Pool is a thread pool written in C#. It is far more advanced than the .NET built-in thread pool.
Here is a list of the thread pool features:

* The number of threads dynamically changes according to the workload on the threads in the pool.
* Work items can return a value.
* A work item can be cancelled.
* The caller thread’s context is used when the work item is executed (limited).
* Usage of minimum number of Win32 event handles, so the handle count of the application won’t explode.
* The caller can wait for multiple or all the work items to complete.
* Work item can have a PostExecute callback, which is called as soon the work item is completed.
* The state object, that accompanies the work item, can be disposed automatically.
* Work item exceptions are sent back to the caller.
* Work items have priority.
* Work items group.
* The caller can suspend the start of a thread pool and work items group.
* Threads have priority.
* Can run COM objects that have single threaded apartment.
* Support Action and Func delegates.
* Support for WindowsCE (limited)
* The MaxThreads and MinThreads can be changed at run time.
* Cancel behavior is imporved.

Mono Tools for Visual Studio Preview Released


Mono Tools for Visual Studio Preview Released

Mono Tools for Visual Studio have been released to a limited number of developers for a closed preview cycle.

This first release of the Mono tools integrates four major pieces of new functionality into Visual Studio:

Scan for Mono Compatibility (MoMA integration) – The integrated Mono Migration Analyzer (MoMA) can scan open projects for incompatibilities and guide you to directly to them, making it easy to find and work through issues as you develop.
Run on Mono on Windows – Testing against Mono on Windows can help isolate issues arising from differences between Mono and .NET.
Run on Mono on Linux – Testing against Mono on Linux helps work through issues that could be caused by differences in Windows and Linux.
Debug on Mono on Linux – Often, the best way to work through an issue will be to debug the application on the target environment. Debug on Mono on Linux brings this functionality to Visual Studio developers by enabling remote debugging of Mono applications running on Linux.

If you are interested in joining the program during the next wave of invitations, please visit the Mono Tools for Visual Studio website, and follow the “Sign Up!" link.

SuperSocket

SuperSocket, an extensible socket application framework – Home

SuperSocket is a light weight, cross platform and extensible socket application framework. You can use it to build a server side socket application (like FTP server, SMTP/POP3/IMAP4 server, SIP server, etc) easily without thinking about how to use socket, how to maintain the socket connections and how socket works(synchronize/asynchronize).

It is a pure C# project which is designed to be extended, so it is easy to be integrated to your existing system. If your systems (like forum/CRM/MIS/HRM/ERP) are developed in .NET language, you must be able to use SuperSocket to build your socket application as a part of your current system perfectly.

I know there are two famous NIO open source frameworks named “MINA" and “Netty" in Java World which are used widely by developers. If you want to look for a .NET portion of MINA or Netty, I suggest you try SuperSocket, although SuperSocket is not strong like MINA and Netty but it is very simple and you can use it easily.

NetServ

壞蛋的部落格 » NetServ

NetServ
2010年3月31日 發表評論 閱讀評論
NetServ 介紹

NetServ 是一組以 C# 寫的 Socket Server 類別 , 主要是給程式設計師一個快速實作 Socket Server 的方式

目前版本只做到 TCP/IP Server 的部分 , 內部運作方式採用非同步 Socket 來實作 , 效能還算可以 , 而且已經測試過在 Win32 .NET 2.0 及 Linux 上的 mono 2.4 可以運作

而在 .Net 中的非同步 Socket 有兩種 API , 就是 Socket.BeginXXX/Socket.EndXXX 及 Socket.XXXAsync , 而 NetServ 是採用 BeginXXX 的方式做的 , 因為經過我好多天的測試 , mono 上以 Socket.XXXAsync 的效能極差 … 甚至比 Select 還差 , 所以我才用比較舊的方式實作

The implementation of anonymous methods in C# and its consequences

The implementation of anonymous methods in C# and its consequences : CSharp Feeds

n one place.
Sponsors
Click Here

Tuesday, August 08, 2006

The implementation of anonymous methods in C# and its consequences

by ericgu via Eric Gunnerson’s Compendium : CSharp on 8/8/2006 2:18:00 PM

Raymond wrote a really nice series of posts on this:

Part 1 http://blogs.msdn.com/b/oldnewthing/archive/2006/08/02/686456.aspx
Part 2 http://blogs.msdn.com/b/oldnewthing/archive/2006/08/03/687529.aspx
Part 3 http://blogs.msdn.com/b/oldnewthing/archive/2006/08/04/688527.aspx