0

I'm trying to automate a WebSocket service that denies connection unless you send a user agent with the CONNECT request.

I tried sending the upgrade request with HttpWebRequest and setting User-Agent using the property.

Using Fiddler to debug the request this was sent out:

CONNECT *.*.com:443 HTTP/1.1
Host: *.*.com:443
Connection: keep-alive

How do I add the User-Agent string to the CONNECT request and then upgrade to using WebSocket protocol?

My code so far:

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;
}
NotGI
  • 458
  • 9
  • 21

1 Answers1

0

You cannot use WebRequest to create a websocket connection. You will need ClientWebSocket and use `ClientWebSocket.Options.SetRequestHeader.

Note, you may have issues adding that header: Setting "User-Agent" HTTP header in ClientWebSocket

Update: Since you cannot add that header with ClientWebSocket try with Websocket4Net.

Community
  • 1
  • 1
vtortola
  • 34,709
  • 29
  • 161
  • 263