C# 如何在discord webhook中发送文件?

C# 如何在discord webhook中发送文件?,c#,discord,C#,Discord,因此,我正在用c#编写一个应用程序,它应该通过Discord Webhook发送消息。我希望它能够发送文件。有可能吗?我该怎么做?下面是我在没有任何文件的情况下发送普通消息的代码 sendWebHook ((webHook), string.Concat(new string[] {"test message", }), "webhook test"); 如果有人能帮助我,我会很高兴的。 :)我检查了API,这似乎是可能的。看 您需要精确标题:Content-Type:multipart/fo

因此,我正在用c#编写一个应用程序,它应该通过Discord Webhook发送消息。我希望它能够发送文件。有可能吗?我该怎么做?下面是我在没有任何文件的情况下发送普通消息的代码

sendWebHook ((webHook), string.Concat(new string[] {"test message", }), "webhook test");
如果有人能帮助我,我会很高兴的。
:)

我检查了API,这似乎是可能的。看

您需要精确标题:
Content-Type:multipart/form-data

我想你需要精确的文件目录。(请参见文件对象引用)

信息:
我不知道如何用C#编写代码,但有一个带有函数的nodejs包。所以这绝对是可能的。

你可以发送文件,你可以这样做

该脚本是为统一而设计的,但很容易更改并应用于其他地方:)

private static readonly Encoding Encoding=Encoding.UTF8;
公共静态HttpWebResponse MultipartFormDataPost(字符串姿势、字符串用户代理、字典后参数)
{
string formDataBoundary=string.Format(“------------{0:N}”,Guid.NewGuid());
string contentType=“多部分/表单数据;边界=“+formDataBoundary;
byte[]formData=GetMultipartFormData(后参数,formDataBoundary);
返回PostForm(postrl、userAgent、contentType、formData);
}
私有静态HttpWebResponse PostForm(字符串postrl、字符串userAgent、字符串contentType、字节[]formData)
{
HttpWebRequest-request=WebRequest.Create(postrl)为HttpWebRequest;
if(请求==null)
{
LogWarning(“请求不是http请求”);
抛出新的NullReferenceException(“请求不是http请求”);
}
//设置请求属性。
request.Method=“POST”;
request.ContentType=ContentType;
request.UserAgent=UserAgent;
request.CookieContainer=新的CookieContainer();
request.ContentLength=formData.Length;
//将表单数据发送到请求。
使用(Stream requestStream=request.GetRequestStream())
{
Write(formData,0,formData.Length);
requestStream.Close();
}
将request.GetResponse()作为HttpWebResponse返回;
}
私有静态字节[]GetMultipartFormData(字典后参数,字符串边界)
{
Stream formDataStream=new System.IO.MemoryStream();
bool needsCLRF=假;
foreach(后参数中的var参数)
{
如果(需要SCLRF)
formDataStream.Write(encoding.GetBytes(“\r\n”)、0、encoding.GetByteCount(“\r\n”);
needsCLRF=true;
if(param.Value为FileParameter)
{
FileParameter fileToUpload=(FileParameter)param.Value;
string header=string.Format(“--{0}\r\n内容配置:表单数据;名称=\“{1}\”;文件名=\“{2}\”\r\n内容类型:{3}\r\n\r\n”,
边界,
参数键,
fileToUpload.FileName??参数键,
fileToUpload.ContentType??“应用程序/八位字节流”);
Write(encoding.GetBytes(header),0,encoding.GetByteCount(header));
formDataStream.Write(fileToUpload.File,0,fileToUpload.File.Length);
}
其他的
{
string postData=string.Format(“--{0}\r\n内容配置:表单数据;名称=\“{1}\”\r\n\r\n{2}”,
边界,
参数键,
参数值);
Write(encoding.GetBytes(postData),0,encoding.GetByteCount(postData));
}
}
//添加请求的结尾。以换行符开头
string footer=“\r\n-->+boundary+”-->\r\n”;
Write(encoding.GetBytes(footer),0,encoding.GetByteCount(footer));
//将流转储到一个字节[]
formDataStream.Position=0;
byte[]formData=新字节[formDataStream.Length];
读取(formData,0,formData.Length);
formDataStream.Close();
返回表单数据;
}
公共类文件参数
{
公共字节[]文件{get;set;}
公共字符串文件名{get;set;}
公共字符串ContentType{get;set;}
公共文件参数(字节[]文件):此(文件,null){}
公共文件参数(字节[]文件,字符串文件名):此(文件,文件名,null){}
公共文件参数(字节[]文件、字符串文件名、字符串内容类型)
{
文件=文件;
FileName=文件名;
ContentType=ContentType;
}
}

实际上,.net 4.5及更高版本中有更简单的方法。上面的示例使用webhook url发送文本文件。你可以根据需要更换

string Webhook\u link=“您的链接在这里”;
字符串FilePath=@“C:\Users\sample.txt”;
使用(HttpClient HttpClient=new HttpClient())
{
MultipartFormDataContent form=新的MultipartFormDataContent();
var file_bytes=System.IO.file.ReadAllBytes(文件路径);
添加(新的ByteArrayContent(文件字节,0,文件字节.Length),“Document”,“file.txt”);
httpClient.PostAsync(Webhook_链接,表单).Wait();
httpClient.Dispose();
}
我曾经使用字节、文件类型、显示文件名来指定文件内容

(new MultipartFormDataContent()).Add(
    ByteArrayContent,                        //fill it with byte array of your file
    File_Type,                               //Specify file type e.g Photo Document etc
    DisplayFilename                          //This name would be shown as your filename
);

请记住,您还需要引用
System.Net.Http.dll

无法通过Discord的webhook发送文件。您只能使用它发送有效负载。图像可以显示在Discord上,但这是通过图像URL实现的。你不是在发送图像,好的。然后我会尝试制作一个机器人,制作一个下载链接
(new MultipartFormDataContent()).Add(
    ByteArrayContent,                        //fill it with byte array of your file
    File_Type,                               //Specify file type e.g Photo Document etc
    DisplayFilename                          //This name would be shown as your filename
);