Monday, December 11, 2017

Powershell - The server committed a protocol violation

The other day I was trying to write a simple crawler to validate some resources against some Urls on a page with Powershell and was getting the error: "The server committed a protocol violation"

After googling around, I found three different solutions for this problem (you can find all references I used to solve this issue in the end of this post):
  1. Modifying your powershell.exe.config;
  2. Modifying your request in order to avoid it stopping if your url is invalid in .Net
  3. Modifying your request in order to avoid it stopping if your url is invalid in .Powershell.

Solution 1 - Modifying powershell.exe.config

Please note that this solution will apply to all powershell requests. Use with caution. Also, this solution is not portable so, if you plan to run this script on a machine you don't have admin access to, this solution will probably not work.

<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing="true" />
</settings>
</system.net>

Solution 2 - Modifying your request in .Net

To modify your request, have the code below before you actually do your invoke-webrequest:




Solution 3 - Modifying your request in .Powershell

Turns out that in powershell, this is actually a one-liner as described here, how cool is that?

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }

References