我正在尝试自动化拒绝连接的WebSocket服务,除非您向用户代理发送CONNECT请求。
我尝试使用HttpWebRequest发送升级请求,并使用属性设置User-Agent。
使用Fiddler调试发送的请求:
CONNECT *.*.com:443 HTTP/1.1
Host: *.*.com:443
Connection: keep-alive
如何将User-Agent字符串添加到CONNECT请求,然后升级到使用WebSocket协议?
到目前为止我的代码:
public void Login ( Action onEnd = null ) {
var req = CreateUpgradeRequest();
var res = GetResponse(req);
}
private HttpWebRequest CreateUpgradeRequest ( ) {
HttpWebRequest request = WebRequest.Create("https://lobby35.runescape.com/") as HttpWebRequest;
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36";
request.Connection = "Upgrade";
SetWebSocketHeader(request, "Key", "5LENZfSifyj/Rw1ghTvpgw==");
SetWebSocketHeader(request, "Version", "13");
SetWebSocketHeader(request, "Extensions", "permessage-deflate; client_max_window_bits");
SetWebSocketHeader(request, "Protocol", "jagex");
return request;
}
答案 0 :(得分:0)
您无法使用WebRequest
创建websocket连接。您需要ClientWebSocket
并使用`ClientWebSocket.Options.SetRequestHeader。
注意,添加该标题时可能会出现问题:Setting "User-Agent" HTTP header in ClientWebSocket
更新:由于您无法使用ClientWebSocket
添加该标头,请尝试使用Websocket4Net。