C ++ Boost Asio发布HTTP请求PHP

时间:2016-09-01 14:24:04

标签: c++ http post boost-asio

我正在尝试编写一个函数,将发送请求发送到HTTP服务器,php文件。

    int post(string url, string data, string & content){
        try{
            content = "";
            if(substr_count(url, "/") == 0){
                url += "/";
            }
            string host = substr(url, 0, strpos(url, "/"));
            string path = substr(url, strpos(url, "/"));
            boost::asio::io_service io_service;
            tcp::resolver resolver(io_service);
            tcp::resolver::query query(host, "http");
            tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
            tcp::resolver::iterator end;
            tcp::socket socket(io_service);
            boost::system::error_code error = boost::asio::error::host_not_found;
            while(error && endpoint_iterator != end){
                socket.close();
                socket.connect(*endpoint_iterator++, error);
            }
            if(error){
                throw boost::system::system_error(error);
            }
            boost::asio::streambuf request;
            std::ostream request_stream(&request);
            request_stream << "POST " << path << " HTTP/1.0\r\n";
            request_stream << "Host: " << host << "\r\n";
            request_stream << "User-Agent: C/1.0";
            request_stream << "Content-Type: application/x-www-form-urlencoded\r\n";
            request_stream << "Accept: */*\r\n";
            request_stream << "Content Length: " << data.length() << "\r\n";
            request_stream << "Connection: close\r\n\r\n";
            request_stream << data;
            boost::asio::write(socket, request);
            boost::asio::streambuf response;
            boost::asio::read_until(socket, response, "\r\n");
            istream response_stream(&response);
            string http_version;
            response_stream >> http_version;
            unsigned int status_code;
            response_stream >> status_code;
            string status_message;
            getline(response_stream, status_message);
            if(!response_stream || http_version.substr(0, 5) != "HTTP/"){
                cerr << "Invalid response\n";
                return 1;
            }
            if(status_code != 200){
                cerr << "Response returned with status code " << status_code << "\n";
                return 1;
            }
            boost::asio::read_until(socket, response, "\r\n\r\n");
            string header;
            while(getline(response_stream, header) && header != "\r"){}
            while(true){
                size_t n = boost::asio::read(socket, response, boost::asio::transfer_at_least(1), error);
                if(!error){
                    if(n){
                        stringstream ss;
                        ss << &response;
                        content += ss.str();
                        last_connection_ip_address = socket.remote_endpoint().address().to_string();
                    }
                }
                if(error == boost::asio::error::eof){
                    break;
                }
                if(error){
                    throw boost::system::system_error(error);
                }
            }
            return 1;
        }catch(exception & e){
            cerr << "Exception: " << e.what() << "\n";
        }
        return 0;
    }

网址格式为myserver / test / post.php,它与POST请求完美配合,因此我认为它不会导致问题。

数据格式:name = test&amp; age = 18。我不确定这是否有效,但这是我用Google搜索时发现的。

post.php中

if(isset($_POST['name'])){
    $name = $_POST["name"];
    $a = fopen("a.txt", "w");
    fwrite($a, $name);
    fclose($a);
    echo $name;
}else{
    echo "0";
}

另外,这就是我使用post函数的方式:

string t;
cout << post("myserver/test/post.php", "name=boxxy&age=18", t);
cout << endl << t;

post函数返回1,但PHP端没有任何反应。它总是返回&#34; 0&#34;,这意味着发布请求不会到达PHP端?

1 个答案:

答案 0 :(得分:1)

这一行:

TabLayout tabLayout = findById(this, R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
tabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager) {
    @Override
    public void onTabReselected(TabLayout.Tab tab) {
        // Scroll to top or whatever
    }
});

最后错过了换行符,即

request_stream << "User-Agent: C/1.0";

因此,内容类型无法正确设置,request_stream << "User-Agent: C/1.0\r\n"; 将为false。