本文整理汇总了C++中HTTPClient::get方法的典型用法代码示例。如果您正苦于以下问题:C++ HTTPClient::get方法的具体用法?C++ HTTPClient::get怎么用?C++ HTTPClient::get使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HTTPClient
的用法示例。
在下文中一共展示了HTTPClient::get方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main() {
MBED_HOSTTEST_TIMEOUT(15);
MBED_HOSTTEST_SELECT(default_auto);
MBED_HOSTTEST_DESCRIPTION(HTTP client hello world);
MBED_HOSTTEST_START("NET_7");
char http_request_buffer[BUFFER_SIZE + 1] = {0};
HTTPClient http;
EthernetInterface eth;
eth.init(); //Use DHCP
eth.connect();
//GET data
{
bool result = true;
const char *url_hello_txt = "http://developer.mbed.org/media/uploads/donatien/hello.txt";
printf("HTTP_GET: Trying to fetch page '%s'...\r\n", url_hello_txt);
HTTPResult ret = http.get(url_hello_txt, http_request_buffer, BUFFER_SIZE);
if (ret == HTTP_OK) {
printf("HTTP_GET: Read %d chars: '%s' ... [OK]\r\n", strlen(http_request_buffer), http_request_buffer);
} else {
printf("HTTP_GET: Error(%d). HTTP error(%d) ... [FAIL]\r\n", ret, http.getHTTPResponseCode());
result = false;
}
if (result == false) {
eth.disconnect();
MBED_HOSTTEST_RESULT(false);
}
}
//POST data
{
bool result = true;
const char *url_httpbin_post = "http://httpbin.org/post";
HTTPText text(http_request_buffer, BUFFER_SIZE);
HTTPMap map;
map.put("Hello", "World");
map.put("test", "1234");
printf("HTTP_POST: Trying to post data to '%s' ...\r\n", url_httpbin_post);
HTTPResult ret = http.post(url_httpbin_post, map, &text);
if (ret == HTTP_OK) {
printf("HTTP_POST: Read %d chars ... [OK]\r\n", strlen(http_request_buffer));
printf("HTTP_POST: %s\r\n", http_request_buffer);
} else {
printf("HTTP_GET: Error(%d). HTTP error(%d) ... [FAIL]\r\n", ret, http.getHTTPResponseCode());
result = false;
}
if (result == false) {
eth.disconnect();
MBED_HOSTTEST_RESULT(false);
}
}
eth.disconnect();
MBED_HOSTTEST_RESULT(true);
}
示例2: httptest
int httptest(CellularModem& modem, const char* apn, const char* username, const char* password)
{
printf("Connecting...\n");
HTTPClient http;
char str[512];
modem.power(true);
Thread::wait(1000);
int ret = modem.connect(apn, username, password);
if(ret)
{
printf("Could not connect\n");
return false;
}
//GET data
printf("Trying to fetch page...\n");
ret = http.get("http://mbed.org/media/uploads/donatien/hello.txt", str, 128);
if (!ret)
{
printf("Page fetched successfully - read %d characters\n", strlen(str));
printf("Result: %s\n", str);
}
else
{
printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
modem.disconnect();
return false;
}
//POST data
HTTPMap map;
HTTPText text(str, 512);
map.put("Hello", "World");
map.put("test", "1234");
printf("Trying to post data...\n");
ret = http.post("http://httpbin.org/post", map, &text);
if (!ret)
{
printf("Executed POST successfully - read %d characters\n", strlen(str));
printf("Result: %s\n", str);
}
else
{
printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
modem.disconnect();
return false;
}
modem.disconnect();
return true;
}
示例3: test
void test(void const*)
{
printf("VodafoneUSBModemHTTPClientTest\n");
VodafoneIOModem modem;
HTTPClient http;
char str[512];
int ret = modem.connect("live.vodafone.com");
if(ret)
{
printf("Could not connect\n");
return;
}else
printf("Connected!\n");
//GET data
printf("Trying to fetch page...\n");
ret = http.get("http://developer.mbed.org/media/uploads/donatien/hello.txt", str, 128);
if (!ret)
{
printf("Page fetched successfully - read %d characters\n", strlen(str));
printf("Result: %s\n", str);
}
else
{
printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
}
//POST data
HTTPMap map;
HTTPText text(str, 512);
map.put("Hello", "World");
map.put("test", "1234");
printf("Trying to post data...\n");
ret = http.post("http://httpbin.org/post", map, &text);
if (!ret)
{
printf("Executed POST successfully - read %d characters\n", strlen(str));
printf("Result: %s\n", str);
}
else
{
printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
}
modem.disconnect();
while(1) {
}
}
示例4: http_access_test
void http_access_test() {
HTTPClient* httpClient = new HTTPClient();
HTTPHeader* httpHeader = new HTTPHeader();
httpHeader->insert(
std::string("Content-Type"),
std::string("application/x-www-form-urlencoded")
);
httpHeader->insert(
std::string("Connection"),
std::string("close")
);
httpClient->get(
new std::string("http://www.ex-studio.info/"),
httpHeader
);
int status_code = httpClient->status_code();
std::cout << "Status code: " << status_code << std::endl;
if(status_code != 200) {
delete httpHeader;
delete httpClient;
return;
}
HTTPHeader* recvHeader = httpClient->header();
std::string* recvBody = httpClient->body();
std::cout << "Date is " << recvHeader->get("Date") << std::endl;
std::cout << "body: " << *recvBody << std::endl;
delete httpHeader;
delete httpClient;
return;
}
示例5: main
int main() {
//initially turn off both the leds.
//Blue LED will be always on because it shares the connection with the SPI clock
led_red = 1;
led_green = 1;
printf("EECS149 email notifier. \r\n");
wifi.init();
while(1) {
//continuosly check connection status
//If not connected, try to set up a connection
if(wifi.is_connected() == false) {
//try to connect
if (wifi.connect() == -1) {
printf("Failed to connect. Please verify connection details and try again. \r\n");
} else {
printf("IP address: %s \r\n",wifi.getIPAddress());
//once connected, turn green LED on and red LED off
led_red = 1;
led_green = 0;
}
} else {
//if the board is connected, fetch the remote file
printf("\r\nTrying to fetch mail info... \r\n");
//GET remote file data
int ret = http.get(MAIL_URL, str, 128);
//analyze ret code
if (!ret) {
printf("Page fetched successfully - read %d characters \r\n", strlen(str));
printf("Result: %s \r\n", str);
num_emails = atoi(str);
if(num_emails == 0) {
//no unread emails, turn the green LED on and red LED off
led_red = 1;
led_green = 0;
} else {
//unread emails. Turn the green LED off and the red one on
led_red = 0;
led_green = 1;
}
} else {
//error fetching remote file
printf("Error - ret = %d - HTTP return code = %d \r\n", ret, http.getHTTPResponseCode());
}
}
//poll the remote file every 5 sec
wait(5.0);
}
}