2

My https POST request works using curl on UNIX with python using the following command:

p=subprocess.Popen(['curl','-s','-k','-X','POST', '-H','Content-Type: application/json','-d',message,<https link>],stdout=subprocess.PIPE)

It can send any string to the web server. Now, I would like have the same on Windows using powershell. I have done the following:

$webclient = new-object system.net.webclient;
$webclient.UploadString("https://host/message","string in json format");

However, it failed:

Exception calling "UploadString" with "2" argument(s): "The underlying connection was     closed: Could not establish trust relationship for the SSL/TLS secure channel."
At line:1 char:24
+ $webclient.UploadString <<<< ("https://host/message","strng in json format");
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException

Is there somethign am I missing? Any help would be greatly appreciated.

Thanks in advance!

Joie

Joie Tamayo
  • 501
  • 3
  • 8
  • 21
  • 1
    Hi all, I made it to work by adding: [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true} Could anyone have a good tutorial about this class? Thanks! – Joie Tamayo May 05 '14 at 23:07
  • 1
    Hi all, I changed the value of [System.Net.ServicePointManager]::ServerCertificateValidationCallback to FALSE but the error still gone. Is this normal ? Also, I checked the msdn site about ServerCertificateValidationCallback and it says, Gets or sets the callback to validate a server certificate. I dont really see the point..Could anyone clarify ? Thanks again! – Joie Tamayo May 05 '14 at 23:40
  • I have a similar question http://stackoverflow.com/questions/24683577/powershell-v2-uploadstring-never-connect – JuanPablo Jul 10 '14 at 18:51

1 Answers1

1

Have you tried Invoke-RestMethod?

Invoke-RestMethod -Uri "https://host/message" -Method Post -ContentType "application/json" -Body '{"string in json format"}' 
logicaldiagram
  • 1,019
  • 11
  • 20