Powershell v3 Invoke-WebRequest HTTPS错误(Powershell v3 Invoke-WebRequest HTTPS error)
使用Powershell v3的Invoke-WebRequest和Invoke-RestMethod我已成功使用POST方法将json文件发布到https网站。
我使用的命令是
$cert=New-Object System.Security.Cryptography.X509Certificates.X509Certificate2("cert.crt") Invoke-WebRequest -Uri https://IPADDRESS/resource -Credential $cred -certificate $cert -Body $json -ContentType application/json -Method POST
但是当我尝试使用GET方法,如:
Invoke-WebRequest -Uri https://IPADDRESS/resource -Credential $cred -certificate $cert -Method GET
返回以下错误
Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send. At line:8 char:11 + $output = Invoke-RestMethod -Uri https://IPADDRESS/resource -Credential $cred + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
我尝试使用以下代码来忽略SSL证书,但我不知道它是否真的做任何事情。
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
有人可以提供一些关于这里可能出现的问题的指导,以及如何解决这个问题?
谢谢
Using Powershell v3's Invoke-WebRequest and Invoke-RestMethod I have succesfully used the POST method to post a json file to a https website.
The command I'm using is
$cert=New-Object System.Security.Cryptography.X509Certificates.X509Certificate2("cert.crt") Invoke-WebRequest -Uri https://IPADDRESS/resource -Credential $cred -certificate $cert -Body $json -ContentType application/json -Method POST
However when I attempt to use the GET method like:
Invoke-WebRequest -Uri https://IPADDRESS/resource -Credential $cred -certificate $cert -Method GET
The following error is returned
Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send. At line:8 char:11 + $output = Invoke-RestMethod -Uri https://IPADDRESS/resource -Credential $cred + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
I have attempted using the following code to ignore SSL cert, but I'm not sure if its actually doing anything.
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
Can someone provide some guideance on what might be going wrong here and how to fix it?
Thanks
原文:https://stackoverflow.com/questions/11696944
最满意答案
结果对于所有这些都是一样的,你会得到一个新的空的不可变数组。 尽管不同的方法有不同的内存管理含义。 使用ARC这最终没有什么区别,但在ARC之前,您必须使用正确的版本或发送适当的保留,发布或自动释放消息。
[NSArray new]
和[[NSArray alloc] init]
返回一个+1保留计数的数组。 在ARC之前,您将不得不释放或自动释放该阵列,否则会泄漏内存。
[NSArray array]
和@[]
返回已经自动释放的数组(保留计数0)。 如果你希望它在没有ARC的情况下继续存在,你必须手动保留它,否则当当前autorelease池被弹出时它会被释放。The result is the same for all of them, you get a new empty immutable array. The different methods have different memory management implications though. Using ARC this makes no difference in the end, but before ARC you would have to use the right version or send appropriate retain, release or autorelease messages.
[NSArray new]
and[[NSArray alloc] init]
return an array with an +1 retain count. Before ARC you would have to release or autorelease that array or you'd leak memory.
[NSArray array]
and@[]
return an already autoreleased array (retain count 0). If you want it to stick around without ARC you'd have to manually retain it or it would be deallocated when the current autorelease pool gets popped.
相关问答
更多-
NSMutableArray *persons = [NSMutableArray array]; for (int i = 0; i < myPersonsCount; i++) { [persons addObject:[[Person alloc] init]]; } NSArray *arrayOfPersons = [NSArray arrayWithArray:persons]; // if you want immutable array 你也可以在不使用NSMutableArray的 ...
-
NSArray.array / .new / @ [] / alloc-init之间的区别(Difference between NSArray.array/.new /@[]/alloc-init)[2024-01-22]
结果对于所有这些都是一样的,你会得到一个新的空的不可变数组。 尽管不同的方法有不同的内存管理含义。 使用ARC这最终没有什么区别,但在ARC之前,您必须使用正确的版本或发送适当的保留,发布或自动释放消息。 [NSArray new]和[[NSArray alloc] init]返回一个+1保留计数的数组。 在ARC之前,您将不得不释放或自动释放该阵列,否则会泄漏内存。 [NSArray array]和@[]返回已经自动释放的数组(保留计数0)。 如果你希望它在没有ARC的情况下继续存在,你必须手动保留它,否 ... -
NSMutableDictionary,alloc,init和reiniting(NSMutableDictionary, alloc, init and reiniting)[2024-03-03]
你永远不会“重新启动”对象。 初始化意味着在新分配的实例上使用,并且可能会在初始化完成后做出假设。 在NSMutableDictionary的情况下,你可以使用setDictionary:用一个新字典或addEntriesFromDictionary:完全替换字典的内容addEntriesFromDictionary:从另一个字典中添加条目(除非存在冲突,否则不会删除当前条目)。 更一般地说,您可以释放该字典并在数组中创建字典的mutableCopy 。 You do not "reinit" object ... -
因为在一开始我用数组声明了所有的数组,如果在某个函数结束时,我返回了使用alloc创建的数组,我必须自动释放该数组,因为内存泄漏问题。 现在使用第一个声明我不需要释放任何东西 当你“销售”一个对象时,这是正确的。 但在其他情况下,当您在iOS上创建一个对象时,您可以在获取现成的自动释放的对象和调用alloc之后进行选择,然后释放,Apple希望您使用alloc和release,因为这会保留对象的生命周期短暂的,在你的控制之下。 这里的问题是,自动释放的对象生活在自动释放池中,并且可以堆积,直到池被排出,每当 ...
-
这里有一些原因: http : //macresearch.org/difference-between-alloc-init-and-new 一些选择的是: new不支持自定义初始化器(如initWithString) alloc-init比new更为明确 一般意见似乎是你应该使用任何你喜欢的东西。 There are a bunch of reasons here: http://macresearch.org/difference-between-alloc-init-and-new Some sel ...
-
我应该总是检查[[NSArray alloc] init ...]是否返回nil?(Should I always check if [[NSArray alloc] init…] returns nil?)[2021-10-30]
不,不是NSArray 。 NSArray是一个使用结构的链表,因此它不会在幕后使用。 检查nil,至少使用NSArray是没有意义的。 但是,如果你使用像CCArray这样的集合类,例如来自cocos2d,那么用大数组检查nil可能是有益的。 尽管如此,iOS上的指针大小是8个字节,甚至1000个元素的C-Array也只有8 KB的RAM。 在大多数情况下,您将无法使用足够的内存来达到用完的程度。 另请注意,如果您的应用程序内存不足,则可以注册许多委托方法以对此进行警告并进行修复。 No, not wit ... -
当我的应用程序在我的任何设备或模拟器上的空白“已保存的照片”专辑中崩溃时,我注意到了这一点。 如果保存的照片中有照片,则不会发生错误。 如果您在模拟器上使用“重置数据和设置”,则很容易复制,并将您的相册留空。 我花了很多年时间试图寻找一种解决方法,但我无法做到。 我认为提交iOS错误报告是一个真正的好主意。 I noticed this when my app crashed on an empty 'Saved Photos' album on any of my devices, or the simu ...
-
后面两种方法都是正确的,但我相信你想把它们放在一起! static NSDateFormatter *sharedInstance = nil; + (NSDateFormatter *)sharedInstance { if (sharedInstance == nil) { sharedInstance = [[NSDateFormatter alloc] init]; } return sharedInstance; } 注意:这样的类被称为单例。 阅读更多 ...
-
编写工厂方法时使用[[self alloc] init](The use of [[self alloc] init] when writing factory methods)[2022-12-14]
instancetype是一个关键字,表示“此方法的返回类型是此方法在其上调用的类的类型”(或子类)。 所以,如果你调用[Baseclass createWithString:] ,返回类型是Baseclass * 。 但是,假设您创建了一个不重写此方法的子类。 如果调用[Subclass createWithString:] ,则返回类型是Subclass * (不是Baseclass * )。 当一个类收到一条消息时, self指向Class对象。 所以当调用[Baseclass createWithS ... -
NSArray alloc和init(NSArray alloc and init)[2021-08-10]
[[NSArray alloc] init]返回一个空的不可变数组。 这没什么好奇怪的。 你还会从一种方法中返回什么意思“没有物品?” 编译器如何知道RAM中的对象计数或大小 对象计数为0.它是空的。 它在RAM中的大小是空NSArray的大小。 NSArray是一个对象,因此它有一些标准的objc_object开销,以及NSArray一些实例变量。 也就是说,作为一个实现细节,Cocoa优化了这一点,并且总是为所有空的NSArray对象返回相同的单例。 您可以通过以下方式演示: NSArray *a = ...