I am interested in scripting a HTTP Request to a listening web service I wrote (for testing purposes). I want to do this in Powershell. Initiating a HTTP Request through telnet is easy enough.
telnet google.com 80
Then, after a socket is established I can type in the following HTTP Verb and file:
GET /search?q=test
This is equivalent to performing a google search for the word "test". Great! However, I need to provide a more detailed HTTP Request Header if I want to do something more interesting. I'm trying to do something very similar to the example found here: http://blog.tonycode.com/tech-stuff/http-notes/making-http-requests-via-telnet
Following the author's example from the previous site, I created a local file with the following contents:
echo "open $1 $2"
sleep 2
echo "GET $4 HTTP/1.0"
echo "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4"
echo "Host: $3"
echo
echo
sleep 2
(This is similar to the custom HTTP Request Header I'd like to use for testing my own web services)
I then save this file as "getpage" (I also tried saving it as "getpage.sh"). I continue following the author's example and attempt to pipe the previous file into telnet from Powershell:
./getpage tonycode.com 80 tonycode.com /| telnet
However, Powershell is returning the following error:
Cannot run a document in the middle of a pipeline: ~\Desktop\getpage.
At line:1 char:1
+ ./getpage tonycode.com 80 tonycode.com /| telnet
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (~....Desktop\getpage:String) [], RuntimeException
+ FullyQualifiedErrorId : CantActivateDocumentInPipeline
I typed $PSVersionTable in Powershell and verified I'm using Powershell version 4.0. I'm starting to run out of ideas. I looked through the StackOverflow forums regarding http requests through telnet but I haven't found a good way to send a custom HTTP Request Header through telnet by passing in a pre-formated http header via a shell pipe. I can't really type the entire Http Request in telnet because 1) that defeats the purpose of scripting these kinds of things and 2) pressing the Enter key from telnet submits the current request before I can finishing typing the full Http Header. Does anyone have any suggestions how I can accomplish this in PowerShell? I can already write and send custom Http Headers in Firefox and it makes life way easier. I would just like to script this in Powershell if possible. Thanks in advance.
I am interested in scripting a HTTP Request to a listening web service I wrote (for testing purposes). I want to do this in Powershell.
then leave telnet alone :)
PowerShell(v3+) has built-in cmdlets:
Or you can use System.Net.Sockets.TcpClient
Or System.Net.WebClient
PS: