0

When executing the code below to get a value for a web server I get this error.

I have read some possible solutions that involve changing setting on the web server it’s self but this is not an option for me because I don’t own the server.

Error

Exception calling "DownloadString" with "1" argument(s): "The server committed a protocol violation. Section=ResponseStatusLine" At C:\get.ps1:28 char:60 + $ramPage = (New-Object System.Net.WebClient).DownloadString <<<< ("http://" + $server + "/RAM/" + $evalRAMStars) + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException

Powershell Code

Function Get-Performance(){
$CPU = Get-WmiObject win32_processor | Measure-Object -property LoadPercentage -Average | Select Average

$SystemInfo = Get-WmiObject -Class Win32_OperatingSystem | Select-Object Name, TotalVisibleMemorySize, FreePhysicalMemory
$perRam = (($SystemInfo.FreePhysicalMemory/1MB) / ($systeminfo.totalvisiblememorysize/1MB)) * 100
$perRam = 100 - $perRam

Write-host ("CPU:  " + $CPU.Average + "%")
Write-host ("Ram:  " + $perRam + "%")

$CPUStars = $CPU.Average / 10
$RAMStars = $perRam /10

if($CPUStars -lt 1){$CPUStars = 1}

Write-host ("{0:N0}" -f $CPUStars)
Write-host ("{0:N0}" -f $RAMStars)

$evalCPUStars = "{0:N0}" -f $CPUStars
$evalRAMStars = "{0:N0}" -f $RAMStars

$server = "10.0.0.188"

#this will get our page for later
$cpuPage = (New-Object System.Net.WebClient).DownloadString("http://" + $server + "/CPU/" + $evalCPUStars)
$ramPage = (New-Object System.Net.WebClient).DownloadString("http://" + $server + "/RAM/" + $evalRAMStars)
Write-host("http://" + $server + "/CPU/" + $evalCPUStars)
Write-host("http://" + $server + "/RAM/" + $evalRAMStars)
}

While($x -ne 0)
{
    Get-Performance
    Start-Sleep -s 10   #this will run the powershell script every 10 seconds.
}
ZnArK
  • 1,533
  • 1
  • 12
  • 23
justinf
  • 1,246
  • 2
  • 19
  • 39

1 Answers1

0

The value of $evalRAMStars is probably causing the unsafe header parsing to block the message. Check that its value is valid. Otherwise, see The server committed a protocol violation. Section=ResponseStatusLine ERROR for more solutions.

Community
  • 1
  • 1
akton
  • 14,148
  • 3
  • 43
  • 47
  • I know there is a valid value in the response. [Setting it programatically:](http://o2platform.wordpress.com/2010/10/20/dealing-with-the-server-committed-a-protocol-violation-sectionresponsestatusline/) seems like it would work but have no idea how to do this in powershell , do you perhaps know ? – justinf Sep 20 '12 at 13:42
  • @justinf Can you do `(New-Object System.Net.WebClient).DownloadString("http://10.0.0.188/RAM/" + $evalRAMStars)` with the value of $evalRAMStars that you are having the issue with? Is $evalRAMStars a number or is there a trailing or leading space or some other character? As for using an app.config file for powershell, see http://stackoverflow.com/questions/17960/powershell-app-config. – akton Sep 20 '12 at 13:53
  • $evalRAMStars contains one charter and that is a 1,I will try just executing (New-Object System.Net.WebClient).DownloadString("http://10.0.0.188/RAM/" + $evalRAMStars) once i get home later.Thanks for your help so far. – justinf Sep 20 '12 at 14:34
  • I tryed to run just this and got the same error `$ramPage = (New-Object System.Net.WebClient).DownloadString('http://10.0.0.188:80/RAM/1')` what it should be returning is `RAM Data received.`If i go to the web site using a normal internet browser i see a white page with that text so it seems to be working just no in powershell – justinf Sep 21 '12 at 05:40
  • @justinf Or the DownloadString method on System.Net.WebClient (http://stackoverflow.com/questions/3142403/c-handling-webclient-protocol-violation). Either way, does the fix mentioned above work? – akton Sep 21 '12 at 06:11
  • I will try them later once I am home ,I only had time to try with the URl. I will read through those links you send me and try them out .Thanks again. – justinf Sep 21 '12 at 06:55
  • I tried this but it did not work I still got the same erro any ideas `$evalRAMStars = 4 $server = "10.0.0.188:80" $ramPage = New-Object System.Net.WebClient $config = New-Object System.net.Configuration.HttpWebRequestElement $config.set_UseUnsafeHeaderParsing($true) $ramPage = (New-Object System.Net.WebClient).DownloadString("http://" + $server + "/RAM/" + $evalRAMStars) ` – justinf Sep 21 '12 at 19:24