// 1111.cpp: 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <cstdlib>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp> enum { max_length = }; class client
{
public:
client(boost::asio::io_service& io_service,
boost::asio::ssl::context& context,
boost::asio::ip::tcp::resolver::iterator endpoint_iterator)
: socket_(io_service, context)
{
//socket_.set_verify_mode(boost::asio::ssl::verify_peer);
socket_.set_verify_mode(boost::asio::ssl::context::verify_none);
socket_.set_verify_callback(
boost::bind(&client::verify_certificate, this, _1, _2)); boost::asio::async_connect(socket_.lowest_layer(), endpoint_iterator,
boost::bind(&client::handle_connect, this,
boost::asio::placeholders::error));
} bool verify_certificate(bool preverified,
boost::asio::ssl::verify_context& ctx)
{
// The verify callback can be used to check whether the certificate that is
// being presented is valid for the peer. For example, RFC 2818 describes
// the steps involved in doing this for HTTPS. Consult the OpenSSL
// documentation for more details. Note that the callback is called once
// for each certificate in the certificate chain, starting from the root
// certificate authority. // In this example we will simply print the certificate's subject name.
char subject_name[];
X509* cert = X509_STORE_CTX_get_current_cert(ctx.native_handle());
X509_NAME_oneline(X509_get_subject_name(cert), subject_name, );
std::cout << "Verifying " << subject_name << "\n"; return preverified;
} void handle_connect(const boost::system::error_code& error)
{
if (!error)
{
socket_.async_handshake(boost::asio::ssl::stream_base::client,
boost::bind(&client::handle_handshake, this,
boost::asio::placeholders::error));
}
else
{
std::cout << "Connect failed: " << error.message() << "\n";
}
} void handle_handshake(const boost::system::error_code& error)
{
if (!error)
{
std::cout << "Enter message: ";
// std::cin.getline(request_, max_length);
size_t request_length = strlen(request_); std::stringstream ss;
ss << "POST " << " https://sandbox.itunes.apple.com/verifyReceipt HTTP/1.1\r\n";
ss << "User-Agent: Fiddler\r\n";
ss << "Host: sandbox.itunes.apple.com\r\n";
ss << "Accept: */*\r\n";
ss << "Connection: close\r\n\r\n"; std::string str = ss.str(); boost::asio::async_write(socket_,
boost::asio::buffer(str, str.length()),
boost::bind(&client::handle_write, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
else
{
std::cout << "Handshake failed: " << error.message() << "\n";
}
} void handle_write(const boost::system::error_code& error,
size_t bytes_transferred)
{
if (!error)
{
/*boost::asio::async_read(socket_,
boost::asio::buffer(reply_, bytes_transferred),
boost::bind(&client::handle_read, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));*/
boost::asio::streambuf response;
boost::asio::read_until(socket_, response, "\r\n");
std::istream response_stream(&response);
std::string http_version;
response_stream >> http_version;
unsigned int status_code;
response_stream >> status_code;
std::string status_message;
std::getline(response_stream, status_message);
if (!response_stream || http_version.substr(, ) != "HTTP/")
{
std::cout << "Invalid response\n";
return ;
}
if (status_code != )
{
std::cout << "Response returned with status code " << status_code << "\n";
return ;
} // Read the response headers, which are terminated by a blank line.
boost::asio::read_until(socket_, response, "\r\n\r\n"); // Process the response headers.
std::string header;
while (std::getline(response_stream, header) && header != "\r")
std::cout << header << "\n";
std::cout << "\n"; // Write whatever content we already have to output.
if (response.size() > )
std::cout << &response; try{ // Read until EOF, writing data to output as we go.
boost::system::error_code error;
char buf[] = { };
while ( socket_.read_some(boost::asio::buffer(buf, ), error)> )
{
boost::system::error_code tmp = error;
std::cout << buf;
} if (error != boost::asio::error::eof)
throw boost::system::system_error(error);
/**
* 21000 App Store不能读取你提供的JSON对象
* 21002 receipt-data域的数据有问题
* 21003 receipt无法通过验证
* 21004 提供的shared secret不匹配你账号中的shared secret
* 21005 receipt服务器当前不可用
* 21006 receipt合法,但是订阅已过期。服务器接收到这个状态码时,receipt数据仍然会解码并一起发送
* 21007 receipt是Sandbox receipt,但却发送至生产系统的验证服务
* 21008 receipt是生产receipt,但却发送至Sandbox环境的验证服务
*/
std::cout << "\r\n\nfinished !!\r\n";
}
catch (std::exception& e)
{
std::cout << "Exception: " << e.what() << "\n";
return;
} }
else
{
std::cout << "Write failed: " << error.message() << "\n";
}
} void handle_read(const boost::system::error_code& error,
size_t bytes_transferred)
{
if (!error)
{
std::cout << "Reply: ";
std::cout.write(reply_, bytes_transferred);
std::cout << "\n";
}
else
{
std::cout << "Read failed: " << error.message() << "\n";
}
} private:
boost::asio::ssl::stream<boost::asio::ip::tcp::socket> socket_;
char request_[max_length];
char reply_[max_length];
}; int main(int argc, char* argv[])
{
try
{ boost::asio::io_service io_service; boost::asio::ip::tcp::resolver resolver(io_service);
boost::asio::ip::tcp::resolver::query query("buy.itunes.apple.com", "");
boost::asio::ip::tcp::resolver::iterator iterator = resolver.resolve(query); boost::asio::ssl::context ctx(boost::asio::ssl::context::sslv23);
ctx.set_verify_mode(boost::asio::ssl::context::verify_none);
//ctx.load_verify_file("ca.pem"); client c(io_service, ctx, iterator); io_service.run();
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
} return ;
}

c++ boost 苹果内购 IAP验证的更多相关文章

  1. 苹果内购服务器验证之receipt返回多组in_app思考

    最近有部分用户反映,苹果内购充值失败,经过测试总结有几个关键点出现问题 1.app购买成功苹果没有返回票据,属于票据遗漏(取决于苹果服务器的响应状况),只能客户端进行监听刷新等处理 2.app连续购买 ...

  2. ios 苹果内购订单验证 --- php实现

    验证函数: function appleVerify($receipt_data,$orderId = 0) { /* * 21000 App Store不能读取你提供的JSON对象 * 21002 ...

  3. php苹果内购订单验证

    /** * 21000 App Store不能读取你提供的JSON对象 * 21002 receipt-data域的数据有问题 * 21003 receipt无法通过验证 * 21004 提供的sha ...

  4. ios 苹果内购订单验证 --- nodejs实现

    实现代码 function IosPlayVerify(data,orderid,cb) { itunesPost(data,function (error,responseData) { if (e ...

  5. iOS开发苹果内购的介绍与实现

    1.iOS开发苹果内购的介绍 1.1 介绍 苹果规定,凡是虚拟的物品(例如:QQ音乐的乐币)进行交易时,都必须走苹果的内购通道,苹果要收取大约30%的抽成,所以不允许接入第三方的支付方式(微信.支付宝 ...

  6. Cocos 2d-X Lua 游戏添加苹果内购(二) OC和Lua交互代码详解

    这是第二篇 Cocos 2d-X Lua 游戏添加苹果内购(一) 图文详解准备流程 这是前面的第一篇,详细的说明了怎样添加内购项目以及填写银行信息提交以及沙盒测试员的添加使用以及需要我们注意的东西,结 ...

  7. iOS:苹果内购实践

    iOS 苹果的内购 一.介绍 苹果规定,凡是虚拟的物品(例如:QQ音乐的乐币)进行交易时,都必须走苹果的内购通道,苹果要收取大约30%的抽成,所以不允许接入第三方的支付方式(微信.支付宝等),当然开发 ...

  8. apicloud含有微信支付。支付宝支付和苹果内购的代码

    apicloud含有微信支付.支付宝支付和苹果内购的代码 <!DOCTYPE html> <html> <head> <meta charset=" ...

  9. 苹果内购和 Apple Pay

    作者:CC老师_MissCC链接:http://www.jianshu.com/p/e3bc47e81785來源:简书 苹果内购 1.什么是内购? 如果你购买的商品,是在本app中使用和消耗的,就一定 ...

随机推荐

  1. java 集合 Se HashTreeSet

    Set接口  Set是Collection的子接口,与List相对 Set集合中的元素的特点是1,无序性 2,无下标3,无重复的元素 Set是个接口,所以无法直接创建对象,要依赖它的实现类来创建对象  ...

  2. 安装linux版zabbix客户端

    安装linux版zabbix客户端 一.下载客户端 查看centos系统内核版本 cat /proc/version 如上图,就选择Linux 2.6系统对应的agent版本程序 打开官网:https ...

  3. Physical (Raw) Versus Logical Backups

    [Physical (Raw) Versus Logical Backups] Physical backups consist of raw copies of the directories an ...

  4. 【OpenGL】第一个窗口

    包含头文件: #include <GL/glew.h> // GLFW #include <GLFW/glfw3.h> 初始化与配置GLFW: glfwInit(); //初始 ...

  5. 如何将div中的内容设置为空同时还要保留div本身

    将div的innerHTML置为空即可,下面有2类方法可以实现: 假设有如下的html片段: <div id="test">这是要删除的内容,还要保留test本身< ...

  6. LeetCode解题思路

    刷完题后,看一下其他人的solution,受益匪浅. 可以按不同的topic刷题,比如数组.字符串.集合.链表等等.先做十道数组的题,接着再做十道链表的题. 刷题,最主要的是,学习思路. 多刷几遍.挑 ...

  7. openvpn 使用账户密码认证

    OpenVPN使用user/passwd完成验证登录 OpenVPN使用user/passwd完成验证登录1,为什么要使用user/passwd?比常规openvpn管理方便,删除用户只需要在pwd. ...

  8. yaml文件 *.yml 写法简介

    YAML(Yet Another Markup Language)(发音 /ˈjæməl/ ) 一种基于Unicode容易阅读,容易和脚本语言交互的,用来表达资料序列的编程语言. 适应场景 脚本语言: ...

  9. CentOS 下搭建Hudson

    1.下载Hudson安装包 wget http://ftp.jaist.ac.jp/pub/eclipse/hudson/war/hudson-3.3.3.war 2.执行 java -jar hud ...

  10. Google、微软软件测试之道

    扫码时备注或说明中留下邮箱 付款后如未回复请至https://shop135452397.taobao.com/ 联系店主