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

New 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?

0 Replies