【发布时间】:2021-02-03 18:00:18
【问题描述】:
我遇到了一个问题,即对 WebService 的调用失败并出现错误 400 - 请求 URL 无效,但前提是请求长度超过 310 个字符。
这是服务器主机设置:
WebServiceHost serviceHost = new WebServiceHost(typeof(DataService), new Uri(externalCommsEndpoint));
WebHttpBinding restBinding = new WebHttpBinding
{
CloseTimeout = TimeSpan.MaxValue,
OpenTimeout = TimeSpan.MaxValue,
ReceiveTimeout = TimeSpan.MaxValue,
SendTimeout = TimeSpan.MaxValue,
MaxBufferPoolSize = 2147483647,
MaxReceivedMessageSize = 2147483647,
ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas()
{
MaxArrayLength = 2147483647,
MaxBytesPerRead = 2147483647,
MaxDepth = 2147483647,
MaxNameTableCharCount = 2147483647,
MaxStringContentLength = 2147483647
}
};
serviceHost.AddServiceEndpoint(typeof(IExternalComms), restBinding, externalCommsEndpoint);
serviceHost.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true });
serviceHost.Open();
这是 UriTemplate:
[OperationContract]
[WebGet(UriTemplate = "createplan/{designid}/{accesstoken}/{contexttype=main}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
Results CreateDesign(string designId, string accessToken, string contextType);
一个失败的示例 GET 请求,因为它的长度为 311 个字符:
http://127.0.0.1:7010/openit/createiton/NXL7-xTYU/eyJrgWQiOiJzaGRkZWZhdWx0IiwidHlwIjoiSldUIiwiYWxnIjoiSFMyNTYifQ.eyJleHAiOjE2MTI0NTUwOTYsInN1YiI6ImxvZ2luIiwiaXNzIjoiaHR0cDovL2xvY2FsaG9zdDoxMjIwMS9jaXMvb2F1dGgvcmVhbG1zL2twc3dlYi1hcHAiLCJpYXQiOjE2MTIzNjg2OTYsInByZWZlcnJlZF91c2VybmFtZSI6Imtwcy1hZG1pbiIsInRlbmFuds
我已尝试增加所有绑定值(如示例代码所示),但这没有任何效果,有人知道为什么会出现 400 错误吗?
【问题讨论】:
-
响应是否有换行符?
-
不,我应该再调查一下,因为我发现默认情况下,没有单个 URL 段可以有超过 260 个字符,它需要更改为查询字符串才能工作。