Getting HTTP error 400- Header too long error - iis

I am getting HTTP 400- Header too long error, I have tried most of solution over internet, but none of them seemed to be working (solutions are like adding reg entries, etc).
I have a sample web application which is calling a web api on my system, but I am getting below error in HTTPERR log file:
2019-11-16 16:58:59 ::1%0 1213 ::1%0 80 HTTP/1.1 GET
/WebApplication2/api/values - 400 - RequestLength -
Here is my code which is calling the web API:
HttpWebRequest GETRequest = (HttpWebRequest)WebRequest.Create(URL);
GETRequest.Method = "GET";
GETRequest.ContentType = "application/json";
GETRequest.Headers.Add("Authorization", "<Something Big value>");
WebResponse GETResponse = await GETRequest.GetResponseAsync();

It is recommended to capture ETL log via
https://blogs.msdn.microsoft.com/wndp/2007/01/18/event-tracing-in-http-sys-part-1-capturing-a-trace/
Then you could use network monitor to analyze both request payload and etl log.
It will help you find the root cause.
Besides, can you find the 400 error in IIS log? Now that the 400 error appear in httperr log. It looks like http.sys blocked the request before it reach IIS server.
If you can find the 400 error in IIS , then you could try to modify the limitation in system.web/httpRuntime and request filter.
If the 400 error only can be found in httperr log. Then you may have to try to make some change in http.sys registry.
https://support.microsoft.com/en-us/help/820129/http-sys-registry-settings-for-windows
Please remember to reboot server to activate these registry!

Related

Azure app service some requests returns 400 Bad Request. The request could not be understood by the server due to malformed syntax

So we have a simple .net core 5.0 service that only serves some simple pages with mvc. We are starting to get 400 Errors (details below) on some of the requests. Our frontend is embedded in an iframe which forces us to use our own domain for our api-calls. The 400 errors disappears when we use the azure internal-urls. (*.azurewebsites.net instead of *.ourdomain.net). When I get to the "diagnose and solve problems" -> "availability and performance" -> HTTP 4XX ERRORS i can se below errors. Any ideas on what can cause this error?
Bad Request. The request could not be understood by the server due to malformed syntax. The client should not repeat the request without modifications.
So, the biggest problem above is that we do not get the correct errormessage. After a lot of experimentation we activated the ConnectionLogging for Kestrel.
WebHost.CreateDefaultBuilder(args)
.UseKestrel(options =>
{
options.ConfigureEndpointDefaults(listenOptions =>
{
listenOptions.UseConnectionLogging();
});
})
And after that we found some more intressting logs. One that said:
Connection id "0HMFSA73IA4LS" bad request data: "Malformed request: invalid headers."
Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException: Malformed request: invalid headers.
After some more investigation we could diff a succesful request from a failing request. And the problem was related to the certificate of *.ourdomain.se. In a part of the certificate we hade a string thats named "Stockholms län" in the cases where it failed the string decoded to l�n and when it succeeded the string decoded to l%C3%A4n. We are now investigating if this is a load balancer problem. But this app is running hostingmodel outofprocess. By changing this to inprocess and wrap our Kestrel in IIS the errors disapears.

Azure App Service Hijacks Response for HTTP STATUS 500 Errors

I have an app service that is working as expected in that I can get to pages, log in, and perform a search.
In the event of errors I have the below code in place in the MVC Controller (not an API endpoint)
{
_log.Error("Customer Search Failed", ex);
Response.StatusCode = 500;
return new JsonResult()
{
Data = new { Success = false, Error = "An error occured.", Amount = 0 },
JsonRequestBehavior = JsonRequestBehavior.AllowGet,
ContentEncoding = System.Text.Encoding.UTF8
};
}
Locally, I get the following response along with an HTTP Status Code of 500:
{"Success":false,"Error":"An error occured.","Amount":0}
In Azure, though, I get this response:
The page cannot be displayed because an internal server error has occurred.
I have written a test app to reproduce this in my own Azure environment so that there wouldn't be anything company Azure Resources that could cause this and I got the same behavior. No application gateways, VNets, anything like that. The app service itself is effectively seeing the 500 HTTP Status Code and then sending its own response vs what I want to send.
Does anyone know why this is happening and/or how to prevent this from happening?
The problem was that Azure must set up standard responses for non-200 HTTP Status Codes. This overrode what my Controllers were returning.
I had to explicitly remove the 500 httpHeader via the web.config in order to see the JSON response I was expecting.
<httpErrors errorMode="Detailed">
<remove statusCode="500" />
</httpErrors>

403 when calling API from Azure App Service

I have a strange problem. I have a .NET Core App which works fine on local machine and passes unit tests.
Inside the app it basically calls our platform web service:
using( WebClient client = new WebClient() )
{
NetworkCredential creds = new NetworkCredential(_userName, _password);
CredentialCache credCache = new CredentialCache();
credCache.Add(new System.Uri(_baseUrl), "Basic", creds);
client.Credentials = credCache;
var url = _baseUrl + "/api/v1/Pricing/Rates";
client.Headers.Add(HttpRequestHeader.ContentType, "application/json");
var request = JsonConvert.SerializeObject(data);
System.Console.Out.WriteLine(request);
var response = client.UploadString(url, request);
var responseObject = JObject.Parse(response);
var products = responseObject["PricingProducts"].Children();
var result = new Dictionary<string, double>();
foreach( var product in products )
{
result.Add(product.Value<string>("LoanProgramName"),
product.Value<double>("Rate"));
}
return result;
}
When I execute this on local machine using dotnet run, everything works fine. Unit tests work great too. The logs on the App Service don't tell me much except that I am getting a 403 from the platform web service.
ers.RatesController.Get (AlexaRates) with arguments ((null)) - ModelState is Valid
2018-02-24 06:37:44.418 +00:00 [Information] Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker: Executed action AlexaRates.Controllers.RatesController.Get (AlexaRates) in 201.3483ms
2018-02-24 06:37:44.447 +00:00 [Error] Microsoft.AspNetCore.Server.Kestrel: Connection id "0HLBRA4B41EO8", Request id "0HLBRA4B41EO8:00000002": An unhandled exception was thrown by the application.
System.Net.WebException: The remote server returned an error: (403) Forbidden.
at System.Net.HttpWebRequest.GetResponse()
at System.Net.WebClient.GetWebResponse(WebRequest request)
at System.Net.WebClient.DownloadBits(WebRequest request, Stream writeStream)
at System.Net.WebClient.UploadBits(WebRequest request, Stream readStream, Byte[] buffer, Int32 chunkSize, Byte[] header, Byte[] footer)
at System.Net.WebClient.UploadDataInternal(Uri address, String method, Byte[] data, WebRequest& request)
at System.Net.WebClient.UploadString(Uri address, String method, String data)
at Rates.RetrieveLatest() in D:\home\site\repository\AlexaRates\Rates.cs:line 50
at AlexaRates.Controllers.RatesController.Get() in D:\home\site\repository\AlexaRates\Controllers\RatesController.cs:line 22
at lambda_method(Closure , Object , Object[] )
at Microsoft.Exten
Has anyone experienced anything similar? I see a bunch 403 posts, but they are mostly about people calling a REST API hosted on the service not calling out.
The 403 forbidden error usually means the server understood the request but refuses to authorize it.
According to your error message, it seems that the error happens in Rates class and RatesController class, which you haven’t showed for us. You could set a break point to check the code in these classes by using remote debugging.
You say the project is working fine locally, but get error in Azure, so please make sure you have published all your projects and data sources to Azure. Check whether the ‘_baseUrl ‘ is from Azure. And make sure you have started the Azure App Service.
There may be other causes of 403 forbidden error. Such as page cache and logging in of cookie. You could refer to this article to learn how to fix the 403 Forbidden Error.
Cause of 403 Forbidden Errors
403 errors are almost always caused by issues where you're trying to access something that you don't have access to.
My fix was that I realized that our infrastructure guys added a IP restriction on the azure app. That is why the app was bouncing back with a 403.
I removed the IP restrictions on the "Networking" -> "Access Restrictions" page.
After trying to add headers and doing various other things the end result was the same - I was getting a 403 error on calling out to a web service.
The solution was to convert from a Web App to a VM and deploy the application there using the old school setup. The application worked there.

Error 500 not leaving a "xml" in "Failed Request Tracing Logs" (Azure)?

I have an app in Azure and I'm monitoring that every request is HTTP 200 (OK)
Yesterday I had many errors HTTP 500 and I went to "Diagnose and solve problems > Failed Request Tracing Logs" and for each of these errors there was a .xml with the detailed information.
Thanks to these logs I solved the problem in my app and deployed again. After this all the requests were HTTP 200.
But today something happend and I had 17 errors HTTP 500:
And then I went to "Diagnose and solve problems > Failed Request Tracing Logs" but there are no .xml for these errors.
I thought: maybe Azure is not logging anymore, so I provoked a 404 but the .xml appeared there, so...
Question: Why I don't have 17 xml for these HTTP500 ?
If I turn on "Failed Request Tracing", I can get the failed requests logs, please make sure you have enabled it.
Failed Request Tracing
Failed requests logs
Besides, the failed requests logs should be stored in /LogFiles/W3SVC#########/ folder, you can go to that folder using Kudu Console and check if the logs are there.

pre flight HTTP OPTIONS call timesout with 500 Error sporadically

I have a C# based webAPI hosted on Azure WebApp.
Sporadically, the HTTP OPTIONS call fails with a 500 error.
The OPTIONS call is not handled by our code. So I assume the server handles it by itself.
I tried enabling logs but both event log and web server logs does not print anything other than that it failed with a 500 error.
Any ideas why this would happen?.
Update:
I see only the below error messages in the web server logs. No details
Additionally I added code in my webAPI to handle options call, but the code does not go there. Not sure if I need to enable/disable something in web.confi.
2017-08-31 11:49:39 xxx.mywebsite.com OPTIONS /api/apiname StartDate=2017-08-25&EndDate=2017-08-31&X-ARR-LOG-ID=d86ffe7f-32c9-410b-8116-65833d5af7b9 443 - xxx.xxx.xxx.xxx Mozilla/5.0+(Windows+NT+6.1;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/60.0.3112.113+Safari/537.36 - www.website.com 500 121 0 636 1612 230013

Resources