- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个 HttpListener
它正在等待传入的请求。奇怪的是,当我发送 request
与 HttpClient
或 WebRequest
类,stream
的读取/解码大约需要 350 毫秒,同时发送相同的 request
与 Insomnia
( https://insomnia.rest/ ) 它只需要 500 个滴答声!!
有人可以向我解释我的错在哪里吗?!
private readonly HttpClient _Client = new HttpClient();
private async void HttpClientMethod(string jsonContent)
{
HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Post, _BaseUrl);
message.Content = new StringContent(jsonContent);
HttpResponseMessage result = await _Client.SendAsync(message);
string content = await result.Content.ReadAsStringAsync();
Console.WriteLine(content);
}
private string WebRequestMethod(string jsonContent)
{
WebRequest request = WebRequest.Create(_BaseUrl);
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
string postData = jsonContent;
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
WebResponse response = request.GetResponse();
// Display the status.
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();
return responseFromServer;
}
private void ReadInputStream(HttpListenerRequest request)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
string text;
using (StreamReader reader = new StreamReader(request.InputStream, request.ContentEncoding))
{
text = reader.ReadToEnd();
}
stopwatch.Stop();
}
只需创建一个新的控制台应用
using System;
using System.Net.Http;
using System.Threading.Tasks;
namespace ApiTest
{
class Program
{
static readonly ApiHost _ApiHost = new ApiHost();
static readonly HttpClient _Client = new HttpClient();
static void Main(string[] args) => MainAsync(args).GetAwaiter().GetResult();
static async Task MainAsync(string[] args)
{
_ApiHost.Start();
Console.WriteLine("Host started");
Console.ReadKey();
string jsonContent ="{\"OperationName\":null,\"Query\":\"query {\\r\\n\\t hero{\\r\\n\\t\\t name,\\r\\n\\t\\t id\\r\\n\\t }\\r\\n\\t human(id: \\\"1\\\"){\\r\\n\\r\\n homePlanet,\\r\\n name\\r\\n\\r\\n }\\r\\n }\",\"Variables\":null}";
HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Post, new Uri("http://localhost:5000/"));
message.Content = new StringContent(jsonContent);
HttpResponseMessage result = await _Client.SendAsync(message);
string content = await result.Content.ReadAsStringAsync();
Console.WriteLine(content);
Console.ReadKey();
}
}
}
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
namespace ApiTest
{
public class ApiHost
{
public HttpListener Listener = new HttpListener();
private bool _Stop;
public void Start()
{
Listener.Prefixes.Add("http://+:5000/");
Listener.Start();
Task.Run(() =>
{
Semaphore semaphore = new Semaphore(4, 4);
while (!_Stop)
{
semaphore.WaitOne();
Listener.GetContextAsync().ContinueWith(async (contextTask) =>
{
try
{
semaphore.Release();
HttpListenerContext context = await contextTask.ConfigureAwait(false);
Process(context);
}
catch (Exception ex)
{
Console.WriteLine(ex);
throw;
}
});
}
});
}
private void Process(HttpListenerContext ctx)
{
try
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
string text;
using (StreamReader reader = new StreamReader(ctx.Request.InputStream, ctx.Request.ContentEncoding))
{
text = reader.ReadToEnd();
}
stopwatch.Stop();
using (Stream output = ctx.Response.OutputStream)
{
using (StreamWriter writer = new StreamWriter(output) { AutoFlush = true })
{
writer.Write(text);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
}
为什么我使用内部库时有360ms。当我使用 Insomnia
例如,我没有这种延迟!
最佳答案
关于c# - 使用 HttpClient 或 WebRequest 时,HttpListenerRequest 读取 InputStream 速度较慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47432197/
我正在使用 HttpListener 创建一个非常基本的网络服务器。我已将它设置为使用 SSL,使用 httpcfg 工具绑定(bind)到适当的端口和证书。这似乎工作正常。我现在想使用客户端证书身份
我有一个基于 HttpListener 的简单 http 服务器。 Http 服务器在防火墙后面的本地计算机 (IP: X.X.X.X:10101) 上运行。为了使 Http 服务器对外部请求可见,我
我正在使用 HttpListener 来实现一个非常简单的 http 服务器,它接受来自 Java 客户端的 POST。当客户端调用时,我得到一个 HttpListenerRequest,其中包含所有
我有一个从 html 发起的 HttpListenerRequest那是张贴的。我需要知道如何获取发布的表单值+上传的文件。有谁知道一个例子可以节省我自己做这件事的时间?我用谷歌搜索了一下,但没有找
我一直在创建下载管理器,它从 Chrome 扩展程序接收文件名和下载链接。 在本例中,我决定使用 javascript 后台页面将 XMLHttpRequest 发送到环回,并创建简单的服务器来接收消
我在 MSDN 中搜索我不知道 HttpListenerRequest 的 POST 参数在哪里? 有什么想法吗? *QueryString好像只有get参数没有post 最佳答案 经过几个小时的搜索
我正在使用 HttpListener 为本地主机上使用另一种技术编写的应用程序提供 Web 服务器。该应用程序使用简单的表单提交 (application/x-www-form-urlencoded)
我有一个 HttpListener它正在等待传入的请求。奇怪的是,当我发送 request与 HttpClient或 WebRequest类,stream 的读取/解码大约需要 350 毫秒,同时发送
我是一名优秀的程序员,十分优秀!