我在問這個問題主要是因爲我沒有一些清晰的想法。我相信我瞭解Web服務器的工作原理,但出於某種原因,我得到的結果與預期不同。使用TcpClient發送請求到服務器
所以基本上我想用代碼複製我用真正的Web瀏覽器做的事情。
我有一個名爲Fiddler的程序,充當代理以查看來自Web服務器的所有請求和響應。
1.所以,當我打開我的broser和,則跳轉http://10.10.10.28/tfs:8080
這是顯示的內容:
--------
。 。 。 。而這也正是小提琴手記錄:
當我點擊取消或嘗試登錄其他請求都將被製成,提琴手會記錄更多的數據。我不在乎這一點,知道我只是在模擬這個第一個請求感興趣。
不管怎麼說所以提琴手運輸發射車我們,標題是:
GET http://10.10.10.28/tfs:8080 HTTP/1.1
Host: 10.10.10.28
Connection: keep-alive
Cache-Control: max-age=0
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.83 Safari/537.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
和反應是:
HTTP/1.1 401 Unauthorized
Server: Microsoft-IIS/7.5
WWW-Authenticate: Negotiate
WWW-Authenticate: NTLM
X-Powered-By: ASP.NET
MicrosoftSharePointTeamServices: 12.0.0.6421
Date: Fri, 24 Aug 2012 14:36:22 GMT
Content-Length: 0
Proxy-Support: Session-Based-Authentication
2.終於等到了有趣的部分代碼現在我會想發送相同的頭文件並期望得到相同的響應。 出於某種原因,我得到了不同的迴應!
public static void Main(string[] args)
{
// I save the header bytes recorded from fiddler on a file to make sure I am sending the exact same request
byte[] header = System.IO.File.ReadAllBytes(@"C:\Users\Antonio\Desktop\header");
// create the client
TcpClient client = new TcpClient("10.10.10.28", 8080);
// get the stream so that we can read and write to it
var stream = client.GetStream();
// now that we have the stream wait for the server to respond
WaitForResponse(stream); // waits on a separate thread
// send the request to the header
stream.Write(header, 0, header.Length);
// wait
Console.Read();
}
public static void WaitForResponse(NetworkStream stream)
{
Task.Factory.StartNew(() => {
byte[] buffer = new byte[16384];
int responseLength = stream.Read(buffer, 0, buffer.Length);
string resp = System.Text.UTF8Encoding.UTF8.GetString(buffer, 0, responseLength);
resp = resp; // place breakpoint
});
System.Threading.Thread.Sleep(10); // make sure task starts
}
這裏的響應,我得到:
爲什麼會出現不同的反應?我相信Web服務器使用tcp連接將頁面發送到客戶端。爲什麼我採取的這種方法不起作用?另外爲什麼當我從代碼發送請求到網絡服務器時,提琴手不記錄任何東西? Google Chrome如何連接到Web服務器?我敢打賭,Chrome瀏覽器正在建立一個與Web服務器的tcp連接。
看起來你想指定一個端口,因爲你的URL包含:8080。如果是這種情況,那麼您使用的語法錯誤。使用'http://10.10.10.28:8080/tfs'連接到端口8080,而不是'http://10.10.10.28/tfs:8080'。 – Sjoerd
謝謝!這使它工作! –