Accessing custom HTTP Response Header via C# - c#

I've created an HTTP Response Header in IIS 7. I can see it in Fiddler but I can't get its value in C#.
Response.Headers.ToString() doesn't show the custom HTTP Response Headers.
How can I access its value?
Thanks in advance

If it is IIS that is adding this response header you might not be able to read it in your application as this header might be added much later in the execution pipeline when the ASP.NET application has finished serving the request.

Related

Request.Headers["Referer"] does not exist

I have a solution with two ASP.NET Core MVC projects. One project (Client) is making a request to the other (Server) using HttpClient. When the action in Server receives the request, I want to get the URL of the thing that sent it. Every article I have read purports Request.Headers["Referer"] as the solution, but in my case Headers does not contain a "referer" key (or "referrer").
When receiving the request in Server, how should I find the URL of the Client that sent it?
That is how you you get the referring url for a request. But the referer isn't the thing that sent the request. The referer gets set in the headers by the browser when a person clicks on a link from one website to go to another website. When that request is made by the browser to the new website the request will typically have the Referer header which will contain the url of the prior website.
The receiving server can't get the url of the "client" making the request, remember a typical web browser client isn't at any url. All the receiving server can get is the IP address of the client typically.
Since you have control of the client software, if you wanted you could have the client put whatever info you want in the header of the request before it's sent to the server and the server could then get that info out of the header.
If you're using HttpClient, then it is up to the site making the request to add that header. It isn't added automatically in this case. So: change the code - or request that the code is changed - so as to add the header and value that you expect. If you are proxying through a request, you might get the value from the current request's Referer header, and add that.
Even in the general case of a browser making the request as part of a normal page cycle, you can't rely on it: the Referer header is often deliberately not sent; depending on the browser version, configuration, whether you're going between different domains, whether it is HTTPS or not, and rel markers on a <a href=... such as "noreferrer".

How to modify values of HTTP header of a request permanently

I am referring to the below post.
How to Modify HTTP Header of a request using C#?
Solution provided here is working fine. But when i do the redirection to another page. All the changes done at request header are being lost. Please help
Because when you do a redirect to another page, it causes the browser to send a new request to the redirected page, so it sends a new set of headers. It does not send the modified headers that you made before the redirection.

Get Host of HttpResponseMessage in Windows Store App

I have a Windows Store App (C#) where I am sending a HttpRequest and I want to check if the response I am getting is from a Captive/Limited Access Network or from the actual host specified in the HttpRequest.
So lets say I am sending a request to www.serverA.com
I look at the response of that request and determine if it was success based on the status code.
Imagine the same scenario in a captive network(airport networks/starbucks where they redirect you to a login page):
I am sending a request to www.serverA.com
My request gets redirected
to www.serverB.com/AirPortLoginPage
I get back a response that the
AirportLoginPage loaded successfully with a 200 response
My code sees that as a success because of the 200 status code, but I wanted to know if my original request was successful
So, is there a way to determine the host of the server where the Response Message is coming from?
Two things which can solve your problem:
You can set HttpClientHandler.AllowAutoRedirect property to
false. But if any other code depends on this - you will need to
handle 3xx (redirect) manually.
You can check HttpResponseMessage.RequestMessage. In your example after you will send request to www.serverA.com - this property will have www.serverB.com/AirPortLoginPage

Client HTTP Handling & ETag

I'm working on a small SilverLight application, which uses the WebClient to request a REST service. According to this:
http://msdn.microsoft.com/en-us/library/dd920295(v=vs.95).aspx
no caching should be in place when using the 'Client HTTP' handling....
The REST service I'm calling use ETag, so with no Caching in place, I would not expect, that a second call to my service, would contain the If-None-Match header ??
Also when the REST service returns '304' (NotModified) I still get some data, indicating that the data is returned from the cache....
The REST service also sets the 'Vary' header to 'Accept' (so that the Accept header, should be taken into account when generating the Cache key), but is doesn't seem like this is working....
So I'm a bit confised, whether the caching is there or not.... seems like it's half-ways there...
Can someone clearify??
TIA
Søren
I would suggest not using ETags on a WCF service. In IIS, the service or the directory containing the service needs to include an HTTP response header setting the content to expire immediately so that there is no possibility of caching on the client. This way a call to the service will not result in an HTTP 304 response from the server.

Http status code with a valid xml string response

I am trying to determine the right status code to apply where the response is not empty.
When a user requests a file and the file does not exist in the server. The user is returned an error message in xml format. I have my own logs but I want to try to make it obvious in IIS logs as well.
Currently the IIS logs a status 200 for all responses. But I want to set a different status code is the server does not find the file.
The problem is during unit testing I found the response is empty if the status code is not 200. I have tried (410, 206, 204). So the client does not receive the error message.
Just want to know if there is any status code I can set and also send the error message.
I am using C# ASP.NET Web Service.
Thanks.
Why are you sending HTTP status codes and not your own application status codes?
IIS logs will never record the code that you return in XML, it will only log the status code it receives from the server that serves your web service. Your XML is merely data as far as IIS is concerned, unless you have a special handler or filter or something installed. Otherwise IIS will only concern itself with the values of your HTTP response headers.
EDIT:
When you set HTTP Status Codes manaully the server will still act within the guidelines of the HTTP spec which states that only a 200 will be accompanied by a full response body.
An HTTP/1.1 404 - Not Found is appropriate - and most servers will allow you to return content, since often you want to return a user-readable HTML page to show that you didn't actually hit a real page.

Categories