Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.
}
share|improve this question

1 Answer

up vote 0 down vote accepted

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.

share|improve this answer
I know there is a valid value in the response. Setting it programatically: 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 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("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 5:40
@justinf Or the DownloadString method on System.Net.WebClient (stackoverflow.com/questions/3142403/…). Either way, does the fix mentioned above work? – akton Sep 21 '12 at 6:11
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.