注意:ここで紹介している方法は.NET Framework 2.0以降で有効です。それ以外では、ネットワークモニタを使用するなどの方法が考えられます。ネットワークモニタに関しては、@ITのこちらの記事で詳しく説明されています。また、Wireshark(旧Ethereal)というツールも有名です。それ以外でも、Vectorで検索すれば見つかるでしょう。
WebClientクラスや、HttpWebRequest、HttpWebResponse、FtpWebRequest、FtpWebResponse、Socket、TcpClientクラスなどを使ってインターネット通信を行うアプリケーションを作成する際に、ネットワークのトレースを行う方法を紹介します。
.NET Framework 2.0以降は、非常に簡単な方法が用意されています。「TraceSourceを使用してトレースする、ログに書き込む」で紹介したTraceSourceがすでに用意されています。後はこれを有効にするだけです。
ネットワークのトレースを有効にするには、「TraceSourceを使用してトレースする、ログに書き込む」で紹介しているように、アプリケーション構成ファイルの内容を変更します。
ネットワークトレースを有効にする最も簡単(?)なアプリケーション構成ファイルの内容は次のようになります。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>
<sources>
<source name="System.Net.Sockets" switchName="NetSwitch"/>
<source name="System.Net" switchName="NetSwitch"/>
<source name="System.Net.HttpListener" switchName="NetSwitch"/>
<source name="System.Net.Cache" switchName="NetSwitch"/>
</sources>
<switches>
<add name="NetSwitch" value="Verbose"/>
</switches>
</system.diagnostics>
</configuration>
アプリケーション構成ファイルをこのようにすると、既定のリスナであるDefaultTraceListenerだけが使用されます。よって、トレースの結果はVisual Studioの出力ログに出力されます。
例えば次のようなコードを実行したとき、
Dim wc As New System.Net.WebClient() wc.DownloadFile("http://localhost/index.html", "C:\test.html") wc.Dispose()
System.Net.WebClient wc = new System.Net.WebClient();
wc.DownloadFile("http://localhost/index.html", "C:\\test.html");
wc.Dispose();
出力される結果は、次のようになります。
System.Net Verbose: 0 : [1704] WebClient#5822459::DownloadFile(http://localhost/index.html, C:\test.html)
System.Net Verbose: 0 : [1704] WebRequest::Create(http://localhost/index.html)
System.Net Verbose: 0 : [1704] HttpWebRequest#60684095::HttpWebRequest(http://localhost/index.html#233724127)
System.Net Verbose: 0 : [1704] Exiting HttpWebRequest#60684095::HttpWebRequest()
System.Net Verbose: 0 : [1704] Exiting WebRequest::Create() -> HttpWebRequest#60684095
System.Net Verbose: 0 : [1704] HttpWebRequest#60684095::GetResponse()
System.Net Information: 0 : [1704] Associating HttpWebRequest#60684095 with ServicePoint#46429731
System.Net Information: 0 : [1704] Associating Connection#8963119 with HttpWebRequest#60684095
System.Net Information: 0 : [1704] Associating HttpWebRequest#60684095 with ConnectStream#30923613
System.Net Information: 0 : [1704] HttpWebRequest#60684095 - Request: GET /index.html HTTP/1.1
System.Net Information: 0 : [1704] ConnectStream#30923613 - ヘッダーを送信しています
{
Host: localhost
Connection: Keep-Alive
}。
System.Net Information: 0 : [1704] Connection#8963119 - 受信された状態行: Version=1.1、StatusCode=200、StatusDescription=OK
System.Net Information: 0 : [1704] Connection#8963119 - 受信されたヘッダー
{
Accept-Ranges: bytes
Content-Length: 86
Content-Type: text/html
Date: Fri, 09 Feb 2007 16:44:51 GMT
ETag: "62f978c694cc71:f58"
Last-Modified: Fri, 09 Feb 2007 16:44:21 GMT
Server: Microsoft-IIS/5.1
X-Powered-By: ASP.NET
}。
System.Net Information: 0 : [1704] ConnectStream#49385318::ConnectStream(86 バイトがバッファされました。)
System.Net Information: 0 : [1704] Associating HttpWebRequest#60684095 with ConnectStream#49385318
System.Net Information: 0 : [1704] Associating HttpWebRequest#60684095 with HttpWebResponse#7746814
System.Net Verbose: 0 : [1704] Exiting HttpWebRequest#60684095::GetResponse() -> HttpWebResponse#7746814
System.Net Verbose: 0 : [1704] HttpWebResponse#7746814::GetResponseStream()
System.Net Information: 0 : [1704] ContentLength=86
System.Net Verbose: 0 : [1704] Exiting HttpWebResponse#7746814::GetResponseStream() -> ConnectStream#49385318
System.Net Verbose: 0 : [1704] ConnectStream#49385318::Read()
System.Net Verbose: 0 : [1704] Data from ConnectStream#49385318::Read
System.Net Verbose: 0 : [1704] 00000000 : 3C 68 74 6D 6C 3E 0D 0A-3C 68 65 61 64 3E 0D 0A : ....
System.Net Verbose: 0 : [1704] 00000010 : 3C 74 69 74 6B 65 3E 49-4E 44 45 58 3C 2F 74 69 : INDEX..
..<
System.Net Verbose: 0 : [1704] 00000030 : 62 6F 64 79 3E 0D 0A 3C-70 3E 48 65 6C 6C 6F 21 : body>..Hello!
System.Net Verbose: 0 : [1704] 00000040 : 3C 2F 70 3E 0D 0A 3C 2F-62 6F 64 79 3E 0D 0A 3C :
..