使用HttpClient进行Https调用(Make Https call using HttpClient)
我一直在使用
HttpClient
来使用C#进行WebApi调用。 与WebClient
相比,看起来整齐快捷。 不过,我在停电时Https
困扰。如何使以下代码进行
Https
调用?HttpClient httpClient = new HttpClient(); httpClient.BaseAddress = new Uri("http://foobar.com/"); httpClient.DefaultRequestHeaders.Accept.Clear(); httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml")); var task = httpClient.PostAsXmlAsync<DeviceRequest>("api/SaveData", request);
编辑:上面的代码可以正常进行http呼叫。 但是当我将计划改为https时,它不起作用。 这是获得的错误:
基础连接已关闭:无法为SSL / TLS安全通道建立信任关系。
编辑2:将方案更改为https是:第一步。
如何提供证书和公钥/私钥以及C#请求。
I have been using
HttpClient
for making WebApi calls using C#. Seems neat & fast way compared toWebClient
. However I am stuck up while makingHttps
calls.How can I make below code to make
Https
calls?HttpClient httpClient = new HttpClient(); httpClient.BaseAddress = new Uri("https://foobar.com/"); httpClient.DefaultRequestHeaders.Accept.Clear(); httpClient.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/xml")); var task = httpClient.PostAsXmlAsync<DeviceRequest>( "api/SaveData", request);
EDIT 1: The code above works fine for making http calls. But when I change the scheme to https it does not work. Here is the error obtained:
The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.
EDIT 2: Changing the scheme to https is: step one.
How do I supply certificate & public / private key along with C# request.
原文:https://stackoverflow.com/questions/22251689
最满意答案
如果模块只需要引用类,那么它们不需要完整的类定义,只需要一个前向声明:
namespace g_editor {class LevelEditor;}
任何代码,例如在模块的成员函数中,需要完整的定义(例如,访问它的成员)将必须进入源文件,其中包含标题。 但模块的类定义本身不应该那样。
If the modules just need references to the class, then they don't need the complete class definition, just a forward declaration:
namespace g_editor {class LevelEditor;}
Any code, for example in the module's member functions, which needs the complete definition (e.g. to access members of it) will have to go in a source file, which inlcudes the header. But the module's class definition itself shouldn't need that.
相关问答
更多-
在所有.h文件中使用前向声明。 您可以这样做,因为您只将指针存储为类成员,因此您不需要完整的类声明。 然后,在所有相应的.cpp文件中, #include包含所需类的头文件。 因此,在TreeRoot.h您转发声明ChildA和ChildB 。 在TreeRoot.cpp ,您需要#include ChildA.h和ChildB.h 。 冲洗并重复其他2个课程。 请注意,这将解决您当前的问题,但这种设计充其量只是片状。 Use a forward declaration in all your .h fil ...
-
如果模块只需要引用类,那么它们不需要完整的类定义,只需要一个前向声明: namespace g_editor {class LevelEditor;} 任何代码,例如在模块的成员函数中,需要完整的定义(例如,访问它的成员)将必须进入源文件,其中包含标题。 但模块的类定义本身不应该那样。 If the modules just need references to the class, then they don't need the complete class definition, just a fo ...
-
在目标C中使用extern(Use of extern in Objective C)[2022-04-16]
你会发现extern在Cocoa框架中被广泛使用,并且很难找到一个令人信服的论据,他们的OO被“宠坏了”。 恰恰相反,Cocoa是完全封装的,只能通过extern暴露出必需的东西。 全局定义的常量当然是最常见的用法,但不一定是唯一有效的使用。 使用extern IMO不一定会“破坏”对象方向。 即使在OO中,常常使用可从任何地方访问的变量。 使用extern是在Objective-C中缺少“类变量”(如Java中用static声明的变量)的最常见的解决方法。 它允许您扩展范围,您可以在其范围内引用一个符号, ... -
有趣的是,这适用于x86; 我不会期望它。 dut.void_pointer在double_reference之前初始化,因为它是在代码中首先定义的。 修复是颠倒实例化的顺序: // first initialise the reference double &double_reference = db.double_variable; // then take the pointer, when it has a defined value. static const struct target_str ...
-
添加前向参考。 顺便说一句,没有理由使用三个头文件。 没有其他类没有任何用途,所以把它们全部放入一个标题。 而一个名称空间将会很好。 #ifndef SNAKEBODY_HPP_ #define SNAKEBODY_HPP_ class SnakeHead; // Here class SnakeGame; // and here class SnakeBody { public: SnakeBody(SnakeHead *origin, int left, int x, i ...
-
c ++ extern对象数组(c++ extern object array)[2022-06-01]
将main_header.hpp更改为: extern myClass myArray[]; Change main_header.hpp to be: extern myClass myArray[]; -
如果两个对象都保持对彼此的强烈引用(即它们保持对方),则可能会在您的手中拥有所谓的“保留周期”。 这两个对象都不会被释放,因为另一个对象会强烈地引用它(保留它),所以它永远不会放弃它的引用(释放)另一个对象。 这种情况对于委托人来说很常见,其中一个对象(称为A)创建另一个对象(B)并将其自身设置为B的委托。 如果A对B有强烈的引用,以便B不会被释放,并且B对A也有强烈的引用,那么就有一个参考周期。 为了避免这种情况,对象通常不会保留或保留对其代表的强烈引用。 让B对弱点的引用不是强而且问题消失。 If tw ...
-
C的名称修改规则是不同的.C可以具有与C ++不同的ABI 。 单独这些原因要求您在C代码中嵌入C代码时使用extern "C" 。 即使编译器可以编译C和C ++代码,它也可以为这两种语言使用不同的名称修改规则或ABI。 另外,您的断言“[C代码最有可能......合法C ++代码”并不完全正确,因为随着时间的推移,C和C ++的分歧越来越大。 他们有很多相似之处,但他们也有很多差异。 Name mangling rules are different for C. And C can have a di ...
-
它是std::string上的const ,它提供了内部链接。 引自[3.5程序和链接]:3: 具有命名空间范围(3.3.6)的名称具有内部链接(如果它的名称) - 一个显式声明为const或constexpr的变量,既未显式声明为extern,也未声明为具有外部链接; 要么 It's the const on std::string, it gives s internal linkage. Quote from [3.5 Program and linkage]:3: A name having nam ...
-
这是一个侧面的答案,但如何使用net.sf.json.JSONObject.fromObject(...)来检查循环引用并抛出异常(如果找到)。 此外,您可以配置库以在必要时以不同方式处理循环引用。 您必须为存在于循环关系中的那些类成员编写一个getter,因为这是JSONObject用来创建JSON的。 A bit of a lateral answer, but how about using net.sf.json.JSONObject.fromObject(...) which checks for ...