当前位置: 首页 > news >正文

品牌网站设计制作哪家靠谱/中国企业网

品牌网站设计制作哪家靠谱,中国企业网,做pc端网站信息,vi设计包含什么如何:连接到 HTTP 服务器 (C REST SDK)07/24/2015本文内容通过 C REST SDK (codename "Casablanca"),你可以从 C 应用更加轻松地连接到 HTTP 服务器。 此页显示三个示例。 第一个示例显示创建 HTTP GET 请求和接收响应的基本方法。 第二个示例…

如何:连接到 HTTP 服务器 (C++ REST SDK)

07/24/2015

本文内容

通过 C++ REST SDK (codename "Casablanca"),你可以从 C++ 应用更加轻松地连接到 HTTP 服务器。 此页显示三个示例。 第一个示例显示创建 HTTP GET 请求和接收响应的基本方法。 第二个示例与第一个示例类似,但生成了一个使用自定义标头值的 HTTP 请求。 第三个示例显示如何使用 HTTP PUT 将文件上载到服务器。

在这些示例之后,有一个更完整的示例显示 #include 和 using 语句。

警告

本主题包含有关 C++ REST SDK 1.0 (codename "Casablanca") 的信息。如果你正在从 Codeplex Casablanca 网页 使用更高版本,则使用 http://casablanca.codeplex.com/documentation 上的本地文档。

创建 HTTP GET 请求和接收响应

以下是使用 web::http::client::http_client 类创建 HTTP GET 请求的方法。 web::http::client::http_response 类表示服务器的响应。 有关将 HTTP 响应作为 JSON 数据进行处理的类似示例,请参阅如何:使用 JSON 数据。

// Creates an HTTP request and prints the length of the response stream.

pplx::task HTTPStreamingAsync()

{

http_client client(L"http://www.fourthcoffee.com");

// Make the request and asynchronously process the response.

return client.request(methods::GET).then([](http_response response)

{

// Print the status code.

std::wostringstream ss;

ss << L"Server returned returned status code " << response.status_code() << L'.' << std::endl;

std::wcout << ss.str();

// TODO: Perform actions here reading from the response stream.

auto bodyStream = response.body();

// In this example, we print the length of the response to the console.

ss.str(std::wstring());

ss << L"Content length is " << response.headers().content_length() << L" bytes." << std::endl;

std::wcout << ss.str();

});

/* Sample output:

Server returned returned status code 200.

Content length is 63803 bytes.

*/

}

创建使用自定义标头值的 HTTP GET 请求

// Builds an HTTP request that uses custom header values.

pplx::task HTTPRequestCustomHeadersAsync()

{

http_client client(L"http://www.fourthcoffee.com");

// Manually build up an HTTP request with header and request URI.

http_request request(methods::GET);

request.headers().add(L"MyHeaderField", L"MyHeaderValue");

request.set_request_uri(L"requestpath");

return client.request(request).then([](http_response response)

{

// Print the status code.

std::wostringstream ss;

ss << L"Server returned returned status code " << response.status_code() << L"." << std::endl;

std::wcout << ss.str();

});

/* Sample output:

Server returned returned status code 200.

*/

}

使用 HTTP PUT 将文件上载到服务器

// Upload a file to an HTTP server.

pplx::task UploadFileToHttpServerAsync()

{

using concurrency::streams::file_stream;

using concurrency::streams::basic_istream;

// To run this example, you must have a file named myfile.txt in the current folder.

// Alternatively, you can use the following code to create a stream from a text string.

// std::string s("abcdefg");

// auto ss = concurrency::streams::stringstream::open_istream(s);

// Open stream to file.

return file_stream::open_istream(L"myfile.txt").then([](pplx::task> previousTask)

{

try

{

auto fileStream = previousTask.get();

// Make HTTP request with the file stream as the body.

http_client client(L"http://www.fourthcoffee.com");

return client.request(methods::PUT, L"myfile", fileStream).then([fileStream](pplx::task previousTask)

{

fileStream.close();

std::wostringstream ss;

try

{

auto response = previousTask.get();

ss << L"Server returned returned status code " << response.status_code() << L"." << std::endl;

}

catch (const http_exception& e)

{

ss << e.what() << std::endl;

}

std::wcout << ss.str();

});

}

catch (const std::system_error& e)

{

std::wostringstream ss;

ss << e.what() << std::endl;

std::wcout << ss.str();

// Return an empty task.

return pplx::task_from_result();

}

});

/* Sample output:

The request must be resent

*/

}

完整示例

以下是完整示例。

// basic-http-client.cpp

#include

#include

#include

#include

using namespace web::http;

using namespace web::http::client;

// Creates an HTTP request and prints the length of the response stream.

pplx::task HTTPStreamingAsync()

{

http_client client(L"http://www.fourthcoffee.com");

// Make the request and asynchronously process the response.

return client.request(methods::GET).then([](http_response response)

{

// Print the status code.

std::wostringstream ss;

ss << L"Server returned returned status code " << response.status_code() << L'.' << std::endl;

std::wcout << ss.str();

// TODO: Perform actions here reading from the response stream.

auto bodyStream = response.body();

// In this example, we print the length of the response to the console.

ss.str(std::wstring());

ss << L"Content length is " << response.headers().content_length() << L" bytes." << std::endl;

std::wcout << ss.str();

});

/* Sample output:

Server returned returned status code 200.

Content length is 63803 bytes.

*/

}

// Builds an HTTP request that uses custom header values.

pplx::task HTTPRequestCustomHeadersAsync()

{

http_client client(L"http://www.fourthcoffee.com");

// Manually build up an HTTP request with header and request URI.

http_request request(methods::GET);

request.headers().add(L"MyHeaderField", L"MyHeaderValue");

request.set_request_uri(L"requestpath");

return client.request(request).then([](http_response response)

{

// Print the status code.

std::wostringstream ss;

ss << L"Server returned returned status code " << response.status_code() << L"." << std::endl;

std::wcout << ss.str();

});

/* Sample output:

Server returned returned status code 200.

*/

}

// Upload a file to an HTTP server.

pplx::task UploadFileToHttpServerAsync()

{

using concurrency::streams::file_stream;

using concurrency::streams::basic_istream;

// To run this example, you must have a file named myfile.txt in the current folder.

// Alternatively, you can use the following code to create a stream from a text string.

// std::string s("abcdefg");

// auto ss = concurrency::streams::stringstream::open_istream(s);

// Open stream to file.

return file_stream::open_istream(L"myfile.txt").then([](pplx::task> previousTask)

{

try

{

auto fileStream = previousTask.get();

// Make HTTP request with the file stream as the body.

http_client client(L"http://www.fourthcoffee.com");

return client.request(methods::PUT, L"myfile", fileStream).then([fileStream](pplx::task previousTask)

{

fileStream.close();

std::wostringstream ss;

try

{

auto response = previousTask.get();

ss << L"Server returned returned status code " << response.status_code() << L"." << std::endl;

}

catch (const http_exception& e)

{

ss << e.what() << std::endl;

}

std::wcout << ss.str();

});

}

catch (const std::system_error& e)

{

std::wostringstream ss;

ss << e.what() << std::endl;

std::wcout << ss.str();

// Return an empty task.

return pplx::task_from_result();

}

});

/* Sample output:

The request must be resent

*/

}

int wmain()

{

// This example uses the task::wait method to ensure that async operations complete before the app exits.

// In most apps, you typically don�t wait for async operations to complete.

std::wcout << L"Calling HTTPStreamingAsync..." << std::endl;

HTTPStreamingAsync().wait();

std::wcout << L"Calling HTTPRequestCustomHeadersAsync..." << std::endl;

HTTPRequestCustomHeadersAsync().wait();

std::wcout << L"Calling UploadFileToHttpServerAsync..." << std::endl;

UploadFileToHttpServerAsync().wait();

}

请参见

其他资源

http://www.lbrq.cn/news/100675.html

相关文章:

  • 建设网站的重要性/杭州seo排名
  • 做网站简单吗/企业管理培训班哪个好
  • 郴州建设公司网站/今天重要新闻
  • 北京市建设工程信息网交易网站/怎么利用互联网推广
  • 深圳网站开发建设培训/网站开发建站
  • dw做网站 后台用什么后台/百度平台我的订单
  • 成都网站优化公司哪家好/主流网站关键词排名
  • 做黑网站吗/市场策划方案
  • 深圳市营销型网站建设/网站搭建模板
  • 什么网站可以做任务赚钱/杭州旺道企业服务有限公司
  • 免费的短视频app大全安卓/优化大师在哪里
  • 云南做网站公司哪家好/互联网平台推广
  • 淘宝天猫做网站咨询/常用的网络推广方式有哪些
  • 定制开发网站的公司/一个新产品策划方案
  • seo网站推广公司/百度提交工具
  • 如何做企业交易网站/搜索引擎广告形式有
  • 网站后台管理系统怎么用/页面关键词优化
  • 一次性核酸病毒采样管价格/北京seo百科
  • 域名备案期间 网站访问/实时积分榜
  • 海尔集团的电子商务网站建设/嘉兴seo
  • 一个好的产品怎么推广/申泽seo
  • wordpress 视频 slider/湖南企业seo优化推荐
  • 免费在线网站建设/百度精简版入口
  • 织梦做商城类网站教程/seo优化是什么
  • crm系统软件排名/重庆seo教程
  • 淘宝客做网站教程/9个广州seo推广神技
  • 网站建设 自动生成/百度一下百度主页
  • 一个专门做试题的网站/怎么网上宣传自己的产品
  • 网站建设公司主营业务/新的网站怎么推广
  • 网站开发常用的开发工具/公司网页制作
  • 自定义单线通信协议解析
  • 2026年计算机毕设推荐:基于大数据的慢性肾病数据可视化分析系统技术选型指南【Hadoop、spark、python】
  • Python数据可视化利器:Matplotlib从入门到实战全解析
  • 精算中的提升曲线(Lift Curve)与机器学习中的差别
  • 20250821日记
  • Flink双流join