Để thực sự hiểu được bên trong của giao thức HTTP bạn có thể sử dụng TcpClient lớp:
using (var client = new TcpClient("www.google.com", 80))
{
using (var stream = client.GetStream())
using (var writer = new StreamWriter(stream))
using (var reader = new StreamReader(stream))
{
writer.AutoFlush = true;
// Send request headers
writer.WriteLine("GET/HTTP/1.1");
writer.WriteLine("Host: www.google.com:80");
writer.WriteLine("Connection: close");
writer.WriteLine();
writer.WriteLine();
// Read the response from server
Console.WriteLine(reader.ReadToEnd());
}
}
Một khả năng khác là để activate tracing bằng cách đặt như sau vào số app.config
và chỉ sử dụng WebClient để thực hiện yêu cầu HTTP:
<configuration>
<system.diagnostics>
<sources>
<source name="System.Net" tracemode="protocolonly">
<listeners>
<add name="System.Net"/>
</listeners>
</source>
</sources>
<switches>
<add name="System.Net" value="Verbose"/>
</switches>
<sharedListeners>
<add name="System.Net"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="network.log" />
</sharedListeners>
<trace autoflush="true"/>
</system.diagnostics>
</configuration>
Sau đó, bạn có thể thực hiện một cuộc gọi HTTP:
using (var client = new WebClient())
{
var result = client.DownloadString("http://www.google.com");
}
Và cuối cùng phân tích lưu lượng mạng trong tạo network.log
tập tin. WebClient
cũng sẽ thực hiện theo các chuyển hướng HTTP.
Nguồn
2010-01-21 14:01:38
Ví dụ tuyệt vời, tôi rất mong được dùng thử. Đây là một cách tuyệt vời để hình dung những gì đang xảy ra. Cảm ơn – Jeff