gpt4 book ai didi

c# - REST 网络服务 "Bad request"

转载 作者:太空宇宙 更新时间:2023-11-03 14:04:58 25 4
gpt4 key购买 nike

我正在编写一个具有如下方法的 REST 网络服务:

[WebGet(
UriTemplate = "/Test/{p1}/{p2}",
BodyStyle = WebMessageBodyStyle.Bare,
ResponseFormat = WebMessageFormat.Xml)]
public string Test(string p1, string p2)
{
// Do something here
}

所以如果我调用 basurl/Test/prova/test我的方法Test使用 p1="prova"p2="test" 调用,一切正常。
当我尝试使用具有(例如)% 字符的参数时,问题就来了:当我尝试调用 basurl/Test/prova/te%25st 时,甚至将其翻译成 URL 代码我得到一个
错误 HTTP 400 - 错误请求。

如果我用

[WebGet(
UriTemplate = "/Test/{p1}?p2={p2}",
BodyStyle = WebMessageBodyStyle.Bare,
ResponseFormat = WebMessageFormat.Xml)]
public string Test(string p1, string p2)
{
// Do something here
}

然后调用basurl/Test/prova?p2=te%25st它有效。
为什么?我该怎么做才能让第一个语法起作用?

更新:
看看我的答案和可能的解决方案。
如果有人找到更好的,请发布!!
谢谢

最佳答案

谷歌搜索我刚刚找到了这个链接: http://weblogs.asp.net/imranbaloch/archive/2010/04/23/understanding-400-bad-request-exception.aspx他们说:

ASP.NET Restrictions:

After passing the restrictions enforced by the kernel mode http.sys then the request is handed off to IIS and then to ASP.NET engine and then again request has to pass some restriction from ASP.NET in order to complete it successfully.

ASP.NET only allows URL path lengths to 260 characters(only paths, for example http://a/b/c/d, here path is from a to d). This means that if you have long paths containing 261 characters then you will get the Bad Request exception. This is due to NTFS file-path limit.

Another restriction is that which characters can be used in URL path portion.You can use any characters except some characters because they are called invalid characters in path. Here are some of these invalid character in the path portion of a URL, <,>,*,%,&,:,\,?. For confirming this just right click on your Solution Explorer and Add New Folder and name this File to any of the above character, you will get the message. Files or folders cannot be empty strings nor they contain only '.' or have any of the following characters.....

For checking the above situation i have created a Web Application and put Default.aspx inside A%A folder (created from windows explorer), then navigate to, http://localhost:1234/A%25A/Default.aspx, what i get response from server is the Bad Request exception. The reason is that %25 is the % character which is invalid URL path character in ASP.NET. However you can use these characters in query string.

The reason for these restrictions are due to security, for example with the help of % you can double encode the URL path portion and : is used to get some specific resource from server.

所以我开始觉得我的问题是不可能解决的。
我确信这个问题不存在于一些用 PHP 编写并由 Apache 托管的 REST 网络服务中,所以我认为这只是一个 IIS/ASP“安全”限制我找不到解决方法...

更新最终解决方案:
我找到了解决方案 here : 阅读文章以了解所有内容。
你应该知道它可能有风险,所以在使用它之前要三思。

<system.web>
<httpRuntime requestPathInvalidCharacters="" />
<pages validateRequest="false" />
</system.web>

关于c# - REST 网络服务 "Bad request",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9579714/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com