我知道 HttpWebRequest 有一個引數可用于將 URL 重定向設定為 false (request.AllowAutoRedirect = False;)。
直接使用 Socket 連接時這是怎么做的?
我沒有任何代碼可以展示,因為我剛剛開始一個專案的開發之路。
不幸的是,我需要堅持使用 Socket 連接,不能使用 HttpWebRequest、WebClient 或 HTTPClient。:(
uj5u.com熱心網友回復:
沒有套接字重定向之類的東西。
HTTP 重定向是您連接到指定的服務器,發送您的 HTTP 請求,服務器發送一個 HTTP 回應,告訴您轉到不同的 URL。那么你就去那個 URL 吧。
它看起來像這樣:
*** socket opened to example.com
*** client sends this part:
GET /some/address HTTP/1.1
Host: example.com
*** server sends this part:
HTTP/1.1 302 Found
Location: https://other.example.com/some_place?really=yes
*** socket closed to example.com
如果您禁用了自動重定向,這就是您的程式得到的回應。否則,HTTP 庫會繼續運行:
*** socket opened to other.example.com
*** client sends this part:
GET /some_place?really=yes HTTP/1.1
Host: other.example.com
*** server sends this part:
HTTP/1.1 200 OK
Content-Type: text/plain
Content-Length: 11
hello world
*** socket closed to other.example.com
如果您只是使用套接字,您將自己開始這樣做。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/347748.html
