使用C ++连接到后端吗?

时间:2018-10-16 12:08:09

标签: c++ backend

我想编写一个代码来连接到具有用户名和密码的后端。.连接后如何发送用户名和密码?下面是我的代码..我找不到关于如何在代码中实现授权过程的明确解释。我要登录的网站要求输入用户名和密码,然后单击登录按钮。在此先感谢您的帮助

#include <boost/asio.hpp>
#include <iostream>
#include <string>

using namespace boost;

void write_to_socket(asio::ip::tcp::socket& sk, const std::string & msg ) {
std::size_t total_write {0};  // bytes successfully witten
std::size_t sz {msg.size()};

// write_some returns the number of bytes sucessfully written
while (total_write != sz ) {
total_write += sk.write_some(asio::buffer(msg.c_str() + total_write, sz - total_write));
}

}

void read_from_socket(asio::ip::tcp::socket& sk ) {
/* to be coded */
}

int main() {
std::string ip_addr {"ip goes here"};
unsigned short port {80};

try {
// initialise end point
asio::ip::tcp::endpoint ep {asio::ip::address::from_string(ip_addr), port};

std::cout << "End point declared \n";

asio::io_service ios;

// Configure socket with ios object and associated end point
asio::ip::tcp::socket sk{ios, ep.protocol()};

std::cout << "Socket Declared \n";

sk.connect(ep); // connect socket to end point
std::cout << "Socket Connected \n";
int f = 1;


boost::system::error_code ec;




// read response
std::string response;

do {
    char buf[1024];
    size_t bytes_transferred = sk.receive(asio::buffer(buf), {}, ec);
    if (!ec) response.append(buf, buf + bytes_transferred);
} while (!ec);

// print and exit
std::cout << "Response received: '" << response << "'\n";



//write_to_socket(sk, "Hello World\n");

}
catch (system::system_error &e) {
std::cout << "Error: " << e.code() << " " << e.what();
return e.code().value();
}

return 0;
}

0 个答案:

没有答案