尝试在c ++

问题描述

我正在尝试在C ++ / CLI中使用net.http。在c ++中,几乎没有示例,而c#示例并非总是简单地翻译。微软文档倾向于使用C#,并且对C ++的差异有所保留。

当前代码:

using namespace System::Net;
using namespace System::Net::Http;

[...]
        try 
        {
            HttpClient^ httpClient = gcnew HttpClient();

            HttpResponseMessage httpResponse =
                httpClient->GetAsync(apiUri)->Result;

            if (httpResponse.IsSuccessStatusCode)
            {
                 // handle results
            }
        }
        catch (Exception ^ ex) 
        {
                 // report error,etc
        }

[...]

此代码未编译,错误为

 error C2664: 'System::Net::Http::HttpResponseMessage::HttpResponseMessage(const System::Net::Http::HttpResponseMessage %)': cannot convert argument 1 from 'System::Net::Http::HttpResponseMessage ^' to 'System::Net::HttpStatusCode'

看起来它使用的是HTTPResponseMessage选择的构造函数中错误的构造函数。

如果我用HttpResponseMessage删除行并做

httpClient->GetAsync(apiUri)->Wait();

我干净地进行编译,并且Web服务器日志显示我的请求到达了那里。但是我需要状态码和返回的正文才能继续,并且我需要能够适当地设置标题。我没有为此找到c ++示例,上面的错误显示了我在翻译c#示例方面的进展。

我注定要失败吗?

/ dps

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)