Hey all I am trying to find some code that will allow me to know what domain the POST request to the web service is coming from.
As an example:
If the web service is on domain:
bob.com/webService/Postsomething
And the client loads a page up on domain:
bill.com/postpage.html
Once the web service is clicked off using AJAX on the html page I want to be able to get the following information from the post function its calling:
bill.com
So far I have only been able to get the IP and host name of where the web service is on and not the client domain they are asking for information from the web service from.
You could use the referrer HTTP header:
public HttpResponseMessage Get()
{
var domain = Request.Headers.Referrer?.GetLeftPart(UriPartial.Authority);
...
}
Of course this header is not guaranteed to be present and you absolutely cannot rely on it because the client making the HTTP request could simply decide not to send it. You should always check if it is null before using it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With