[HttpException (0x80004005): A potentially dangerous Request.Path value was detected from the client

Copper Contributor

Hello everyone,

 

Receiving the above error when creating a HttpResponseMessage with content containing the ampersand symbol. But works well when the message does not contain the symbol. The code is as follows:

 

public static HttpResponseMessage Execute<T>(T content, string uri, bool isPreventUpdate, bool isPreventCreate, HttpMethod httpmethod, HttpClient httpClient)
{
HttpRequestMessage httpRequestMessage = new HttpRequestMessage(httpmethod, httpClient.BaseAddress + WebAPIUrl + uri)
{
Content = new StringContent(SerializationHelper.SerializeToJson(content), Encoding.UTF8)
};
httpRequestMessage.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json;charset=UTF-8");
if (isPreventUpdate)
{
//httpRequestMessage.Headers.TryAddWithoutValidation("If-None-Match", "*");
}
else if (isPreventCreate)
{
httpRequestMessage.Headers.TryAddWithoutValidation("If-Match", "*");
}

httpRequestMessage.Headers.Add("Prefer", "return=representation");
httpRequestMessage.Headers.Add("Accept", "application/json;charset=UTF-8");
HttpResponseMessage response = httpClient.SendAsync(httpRequestMessage).GetAwaiter().GetResult();
if (!response.IsSuccessStatusCode)//&& response.StatusCode != HttpStatusCode.PreconditionFailed
{
CrmHttpResponseException ex = new CrmHttpResponseException(response.Content);
throw ex;
}
return response;
}

 

I tried replacing the symbol(&) by "%26" but does not work. 

Can you please help me on this?

1 Reply

Hi @AkashRam27,

Thanks for posting your issue here.

However this platform is used for how-to discussions and sharing best practices for building any app with .NET.Since your issue is a technical question, welcome to post it in Microsoft Q&A forum, the support team and communities on Microsoft Q&A will help you for any technical questions.
Besides, it will be appreciated if you can share it here once you post this technical question Microsoft Q&A.
Best Regards,
Lan Huang