首页 \ 问答 \ 使用HttpClient进行Https调用(Make Https call using HttpClient)

使用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 to WebClient. However I am stuck up while making Https 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
更新时间:2023-09-28 06:09

最满意答案

如果模块只需要引用类,那么它们不需要完整的类定义,只需要一个前向声明:

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 ...
  • 你会发现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 ...
  • 将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 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。