Author Posts

January 1, 2012 at 12:00 am

by alstersjo at 2012-10-01 01:46:01

Hi
I am developing a surveillence tool that will test if a sharepoint site is responding correctly. To do this I will need to send a http request to the specific servers ip:s since the soulution is load balanced and the URL is pointed to one IP served by multiple servers. And since SHP needs the url to bee specified in Alteranate Accees Mappings i can't test only the servers IP-adress.

Therefore I need to make a http request with the Header HOST set. As far as I have read I can't do this with the system.net.webclient since the HOST header is restricted.
Does anyone know how to perform this?
Best Regards Henrik Alstersjö

by MattG at 2012-10-01 04:37:25

Easy. Invoke-WebRequest won't let you do this but you can alter the host header with System.Net.HttpWebRequest. The following code snippet will send a get request to google.com with a host header of http://www.foo.bar. Any packet capture tool will confirm that the host header was modified.
$Request = [Net.HttpWebRequest]::Create('http://www.google.com/')
$Request.Host = 'www.foo.bar'
$Response = $Request.GetResponse()
I hope this helps. 😀

by alstersjo at 2012-10-03 01:21:22

Hi
Thanks for your answer, but it dosen't seem to work.
When I tries what you suggest I get the errormessage:

Property 'Host' cannot be found on this object; make sure it exists and is settable.
At C:\Users\mossconfig\AppData\Local\Temp\2\tmp34CB.ps1:2 char:10
+ $Request. < <<< Host = 'autosurf.mossopad.com'
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException

I can't find how to set this in powershell the correct way.

by alstersjo at 2012-10-03 07:55:12

Hi Again
After a lot of googling I found out that I needed to force PowerShell to run .Net framework 4 to make the above work.
Thanks again
Regards Henrik

by MattG at 2012-10-03 08:04:04

Right. I'm sorry. I should have mentioned that. I've been getting to accustomed to PowerShell v3. 😀

by alstersjo at 2012-10-04 00:19:12

No problem, you got me on the rigth track and that I am thankful for

by pcgeek86 at 2012-10-04 13:08:38

You can also use the Invoke-WebRequest cmdlet, which has a -Headers parameter. This parameter accepts a [HashTable] as input, and since HTTP headers are nothing more than key/value pairs, the [HashTable] is well-suited for this purpose.

$Headers = @{ Authorization = 'MYOAUTHTOKEN'; };
Invoke-WebRequest -Uri 'http://twitter.com/api/restmethod' -Headers $Headers;

Cheers,
Trevor Sullivan
http://twitter.com/pcgeek86
http://trevorsullivan.net

by MattG at 2012-10-04 19:43:58

Hey Trevor,

While I agree that Invoke-WebRequest would be ideally suited for adding header values, unfortunately it throws an exception when you try to modify the Host header. This is for a valid reason as most people have no legitimate need to modify it.

by alstersjo at 2012-10-25 05:51:57

Hi again
I thougt this was solved, but Since I am going to use this on a Sharepoint farm I get compability error when running .Net 4.0 🙁
So, my question is back
Is there anyone who can help me do this without powershell 3.0 or .net 4.0

I am developing a surveillence tool that will test if a sharepoint site is responding correctly. To do this I will need to send a http request to the specific servers ip:s since the soulution is load balanced and the URL is pointed to one IP served by multiple servers. And since SHP needs the url to bee specified in Alteranate Accees Mappings i can't test only the servers IP-adress.

Therefore I need to make a http request with the Header HOST set. As far as I have read I can't do this with the system.net.webclient since the HOST header is restricted.

by DonJ at 2012-10-25 07:28:16

There may not be. Unfortunately, what you're trying to do is explicitly disallowed, so it may be a dead end.

The "right" approach would be to hit the server by URL, so that the Host header can be set automatically. Now, because of your load balancer, that obviously isn't working, which is why you tried IP. Got it. But, consider binding a second URL to the sharepoint servers for this specific purpose only. That way those can have a unique DNS entry, and you can connect to it as it wants.