我想用C#为DiscordBot编写一个库,但是在发送消息之前,我必须向Disord Gateway发送一个WSS POST请求,该请求无法与标准WebRequest一起使用。
我尝试改用HTTPS,但是WSS是WebRequest无法理解的另一个前缀。使用WebRequest.Registerprefix()也不起作用,但是我认为这可以在正确的界面上工作。
代码如下:
private void Identify(string token)
{
string reqContent = "{"+
"\"token\": \"" + token+ "\","+
"\"properties\": {"+
"\"$os\": \"linux\","+
"\"$browser\": \"disco\","+
"\"$device\": \"disco\"" +
"},"+
"\"compress\": true,"+
"\"large_threshold\": 250,"+
"\"shard\": [1, 10],"+
"\"presence\": {"+
"\"game\": {"+
"\"name\": \"Cards Against Humanity\","+
"\"type\": 0"+
"},"+
"\"status\": \"dnd\","+
"\"since\": 91879201,"+
"\"afk\": false"+
"}"+
"}"; //Example from discord.gg
WebRequest req = WebRequest.Create(
"wss://gateway.discord.gg/?v=" + gatewayVersion + "&encoding=json");
//Here is the Exception
req.Method = "POST";
var bytes = Encoding.ASCII.GetBytes(reqContent);
req.GetRequestStream().Write(bytes, 0, bytes.Length);
WebResponse response = req.GetResponse();
Console.WriteLine(response.GetResponseStream().ToString());
}