当路径包含%7F时,HttpListener返回400错误请求 [英] HttpListener returns 400 Bad Request when path contains %7F
问题描述
我已经设置了System.Net.HttpListener,并且在大多数情况下都可以正常工作.最近,我遇到了路径中某些特定字符组合的问题,例如.../localhost/content/Default/%c2%83%ca%8b%cc%8d.JPG将触发400错误 要求. %c2%83%ca%8b%cc%8d是Unicode字符ƒʋ̍"的url编码.有谁知道为什么HttpListener困扰于URL编码字符?它不应该传递给我的回调,以便我可以处理URL解码
I have set up a System.Net.HttpListener and it works fine in most cases. Recently I have run into problems with certain combinations of urlencoded characters in the path for example .../localhost/content/Default/%c2%83%ca%8b%cc%8d.JPG will trigger 400 Bad Request. %c2%83%ca%8b%cc%8d is the url encoding of the unicode character "ƒʋ̍". Does anyone have a clue why the HttpListener bother about the URL encoded characters? Shouldn't it be passed through to my callback so I can deal with the URL decoding in my code?
推荐答案
您好 马格努斯,
Hi Magnus,
我尝试使用以下代码重现您的问题,但失败并显示错误参数不正确".因此,我无法重现您的问题.您能否为我们提供一个简化的演示,我们将做进一步的研究.
I've tried to reproduce your issue using the following code, but I am failed with error "the parameter is incorrect ". So I am failed to reproduce your issue. Could you provide us a simplified demo, we'll take a further look.
代码段
static void Main(string[] args)
{
SimpleListenerExample(new[] { "http://localhost:8080/index/", "http://localhost:8080/%c2%83%ca%8b%cc%8d.JPG/" });
}
// This example requires the System and System.Net namespaces.
public static void SimpleListenerExample(string[] prefixes)
{
if (!HttpListener.IsSupported)
{
Console.WriteLine("Windows XP SP2 or Server 2003 is required to use the HttpListener class.");
return;
}
// URI prefixes are required,
// for example "http://contoso.com:8080/index/".
if (prefixes == null || prefixes.Length == 0)
throw new ArgumentException("prefixes");
// Create a listener.
HttpListener listener = new HttpListener();
// Add the prefixes.
foreach (string s in prefixes)
{
listener.Prefixes.Add(s);
}
listener.Start();
Console.WriteLine("Listening...");
// Note: The GetContext method blocks while waiting for a request.
HttpListenerContext context = listener.GetContext();
HttpListenerRequest request = context.Request;
// Obtain a response object.
HttpListenerResponse response = context.Response;
// Construct a response.
string responseString = "<HTML><BODY> Hello world!</BODY></HTML>";
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
// Get a response stream and write the response to it.
response.ContentLength64 = buffer.Length;
System.IO.Stream output = response.OutputStream;
output.Write(buffer, 0, buffer.Length);
// You must close the output stream.
output.Close();
listener.Stop();
}
>>%c2%83%ca%8b%cc%8d是Unicode字符ƒʋ̍"的url编码.有谁知道为什么HttpListener会困扰URL编码的字符?
>>%c2%83%ca%8b%cc%8d is the url encoding of the Unicode character "ƒʋ̍". Does anyone have a clue why the HttpListener bother about the URL encoded characters?
此外,从以上消息中,我很好奇您为什么对Unicode字符ƒʋ̍"进行编码.正如我对 http://localhost:8080/ ƒʋ̍/" 成功地.
In addition, from above message, I am curious about why you encoding of the unicode character "ƒʋ̍". As I tested with "http://localhost:8080/ƒʋ̍/" successfully.
如果我误解了您,请随时告诉我.
If I misunderstood you, please feel free to let me know.
祝你有美好的一天!
克里斯汀
Kristin
这篇关于当路径包含%7F时,HttpListener返回400错误请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!