首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将HttpListenerRequest发送到另一个网站

将HttpListenerRequest发送到另一个网站
EN

Stack Overflow用户
提问于 2021-04-05 16:03:54
回答 1查看 224关注 0票数 0

我正试图将收到的http请求转发到另一个网站,下面是我如何接收数据的方法。

代码语言:javascript
复制
HttpListener listener = new HttpListener();
listener.Prefixes.Add("http://localhost:3294/discord/");
listener.Start();
while (true)
{
    Console.WriteLine("Listening...");
    HttpListenerContext context = listener.GetContext();
    HttpListenerRequest request = context.Request;
    //here is want to send data to https://www.discord.com/api/webhooks/#################/###########
}

要发送数据,我使用HttpClient.PostAsync(string requestUri, HttpContent content),但不能以HttpContent的形式发送context.Request

我希望我能提供足够的信息。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-05 18:11:15

代码如下所示:

代码语言:javascript
复制
HttpListener listener = new HttpListener();
listener.Prefixes.Add("http://localhost:3294/discord/");
listener.Start();

Console.WriteLine("Listening...");
HttpListenerContext context = listener.GetContext();

Stream body = context.Request.InputStream;
Encoding encoding = context.Request.ContentEncoding;
byte[] buffer = new byte[body.Length];
body.Read(buffer,0,(int)body.Length);

HttpWebRequest request = (HttpWebRequest)WebRequest.Create ("https://www.discord.com/api/webhooks/#################/###########");
request.Method = "POST";
request.ContentType = context.Response.ContentType;

body.Position = 0;
request.ContentLength = body.Length;
Stream newStream = request.GetRequestStream();
newStream.Write(buffer, 0, buffer.Length);
HttpWebResponse response =  (HttpWebResponse)request.GetResponse();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66955921

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档