Problems posting to http form


I have found a script online which allows me to post to a http page via powershell. The problem i have is that although it shows no errors and seems to get responses back ok, i never actually see any posted data. Anyone got any ideas?

Post the results to a web page


            $webRequest = [System.Net.WebRequest]::Create($url)
            $webRequest.ContentType = "text/html"
            $PostStr = [System.Text.Encoding]::UTF8.GetBytes($detailstoPOST)
            write-host $poststr
            $webrequest.ContentLength = $PostStr.Length
            $webRequest.ServicePoint.Expect100Continue = $false

            $webRequest.Method = "POST"
            $requestStream = $webRequest.GetRequestStream()
            $requestStream.Write($PostStr, 0,$PostStr.length)

            [System.Net.WebResponse]$resp = $webRequest.GetResponse();
            $rs = $resp.GetResponseStream();
            [System.IO.StreamReader]$sr = New-Object System.IO.StreamReader -argumentList $rs;
            [string]$results = $sr.ReadToEnd();
            $HttpWebResponse = $WebRequest.GetResponse();
            write-host "The HTTP response code is"
            Write-Host -Object $HttpWebResponse.StatusCode.value__;

Without knowing what the Web page is doing on the other end, it’s impossible to say. It could be looking for a cookie you haven’t passed, it might not be intended to return any data, it might be checking referrer strings, it could be a million things.

Apologies. That was a bit of a non post wasnt it.

At present im just testing so it’s a quick and dirty little ASP page:

response.write(" " & request(“tp”))
tp = request(“tp”)

Dim fso
Dim tst
Set fso = Server.CreateObject(“Scripting.FileSystemObject”)
Set tst = fso.OpenTextFile(“c:\inetpub\wwwroot\text\reading.txt”, 2, true)
tst.writeline Request.Form(“mac”)
tst.writeline Request.Form(“tp”)

Set tst = Nothing
Set fso = Nothing
This is a test of my <%=tp%> variable
<form name=“input” action=“test.asp” method=“post”>
Username: <input type=“text” name=“mac”>
<input type=“text” name=“tp”>
<input type=“submit” value=“Submit”>

Im posting the following to the form, but no matter what i put in there, it does not show up in the form

$detailstoPOST = “nic=Backup&mac=00:50:56:A4:4B:C4&ip= fe80::e5a2:7720:a830:abed&tp=8Eh<2)<v
110 105 99 61 69 83 89 45 66 97 99 107 117 112 38 109 97 99 61 48 48 58 53 48 58 53 54 58 65 52 58 52 66 58 67 52 38 105 112 61 49 57 50 46 49 54 56 46 50 50 50 46 53 53 32 102 101 56 48 58 58 101 53 97 50 58 55 55 50 48 58 97 56 51 48 58 97 98 101 100 38 116 112 61 56 69 104 60 50 41 60 118”

So, when you do a POST, the form fields go into the HTTP request, but they have to be formatted in a specific way. The server decodes that and, in the case of ASP, give you the Request object. My guess is that the information isn’t going into the right format or spot, so the server doesn’t “see” it. Manually constructing a set of POST headers is a little outside my expertise, though.

So, you’re not technically “posting the following to the form.” You’re posting it to the server. The server doesn’t “insert the values into the form fields;” your actual HTML form tags aren’t being used by your PowerShell code.

This would be a lot easier with a GET. You just call your Web page with URL parameters: mywebpage.asp?nic=xxx&mac=abc123. Then you don’t have to construct POST headers and whatnot. But it still doesn’t “fill in the form fields;” it’s just sending the values AS IF the form had been manually filled out.

Thanks for getting back to me. So how would i do the same with a get request then?

You put the data into the URL, as I suggested.

Invoke-WebRequest -Uri http://myserver/mypage.asp?nic=xxxxx&mac=abcdef1234567

(Invoke-WebRequest is in PowerShell v3 and later; it’s a lot easier than using the WebRequest .NET class directly, but you’re welcome to use the class instead of the cmdlet if you prefer.)

GET just involves putting your data into the URL, as I’ve shown here, rather than building a set of POST headers.


Thanks for getting back to me. Im afraid that my last post was very lazy and a really easy google so apologies if i wasted your time at all.

I used (New-Object System.Net.WebClient).DownloadString(“”) in the end since im needing to support Powershell 2 as well.

I guess the problem that im trying to get around is that i was hoping to post to ssl thereby encrypting the data. If it’s in the URL string then the thats not going to work so well.

This does however kind of of prove that the process is achievable. All be it without the desired end result :slight_smile: