C/C++使用libcurl庫傳送http請求
C++要實現http網路連線,需要藉助第三方庫,libcurl使用起來還是很方便的
環境:win32 + vs2015
如果要在linux下使用,用cmake編譯,使用基本同理
1,下載編譯libcurl
下載curl原始碼,找到vs工程,按照x86 x64 並對應debug和release編譯出靜態庫lib
2,構建工程
1)curl標頭檔案和lib拷貝到工程目錄
2)配置附加包含目錄libcurl中的include和附加庫目錄libcurl中的lib目錄
3)新增預編譯巨集USE_OPENSSL和CURL_STATICLIB
4)新增附加依賴庫
ws2_32.lib wldap32.lib crypt32.lib advapi32.lib libcurl.lib
注意版本對應
3,程式碼示例
#include <iostream> #include <string> #include "curl/curl.h" using namespace std; #pragma comment(lib, "ws2_32.lib") #pragma comment(lib, "wldap32.lib") #pragma comment(lib, "crypt32.lib") #pragma comment(lib, "advapi32.lib") #pragma comment(lib, "libcurl.lib") // reply of the requery size_t req_reply(void *ptr, size_t size, size_t nmemb, void *stream) { cout << "----->reply" << endl; string *str = (string*)stream; cout << *str << endl; (*str).append((char*)ptr, size*nmemb); return size * nmemb; } // http GET CURLcode curl_get_req(const std::string &url, std::string &response) { // init curl CURL *curl = curl_easy_init(); // res code CURLcode res; if (curl) { // set params curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); // url curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false); // if want to use https curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false); // set peer and host verify false curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, req_reply); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response); curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); curl_easy_setopt(curl, CURLOPT_HEADER, 1); curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3); // set transport and time out time curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3); // start req res = curl_easy_perform(curl); } // release curl curl_easy_cleanup(curl); return res; } // http POST CURLcode curl_post_req(const string &url, const string &postParams, string &response) { // init curl CURL *curl = curl_easy_init(); // res code CURLcode res; if (curl) { // set params curl_easy_setopt(curl, CURLOPT_POST, 1); // post req curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); // url curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postParams.c_str()); // params curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false); // if want to use https curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false); // set peer and host verify false curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, req_reply); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response); curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); curl_easy_setopt(curl, CURLOPT_HEADER, 1); curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3); curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3); // start req res = curl_easy_perform(curl); } // release curl curl_easy_cleanup(curl); return res; } int main() { // global init curl_global_init(CURL_GLOBAL_ALL); // test get requery string getUrlStr = "http://cn.bing.com/images/trending?form=Z9LH"; string getResponseStr; auto res = curl_get_req(getUrlStr, getResponseStr); if (res != CURLE_OK) cerr << "curl_easy_perform() failed: " + string(curl_easy_strerror(res)) << endl; else cout << getResponseStr << endl; // test post requery string postUrlStr = "https://www.baidu.com/s"; string postParams = "f=8&rsv_bp=1&rsv_idx=1&word=picture&tn=98633779_hao_pg"; string postResponseStr; auto res = curl_post_req(postUrlStr, postParams, postResponseStr); if (res != CURLE_OK) cerr << "curl_easy_perform() failed: " + string(curl_easy_strerror(res)) << endl; else cout << postResponseStr << endl; // global release curl_global_cleanup(); system("pause"); return 0; }
- get和post可以用於請求html資訊,也可以請求xml和json等串
- 可以新增自定義的header 域和cookies
- 這是libcurl的簡單介面,基本等同於阻塞試請求,libcurl有高階的非同步併發介面,運用更復雜
相關推薦
C/C++使用libcurl庫傳送http請求
C++要實現http網路連線,需要藉助第三方庫,libcurl使用起來還是很方便的 環境:win32 + vs2015 如果要在linux下使用,用cmake編譯,使用基本同理 1,下載編譯libcurl 下載curl原始碼,找到vs工程,按照x86 x64 並對
C++使用curl庫傳送https請求中文亂碼問題
//在資料頭設定字符集為UTF-8,解決中文亂碼問題 struct curl_slist *head = NULL; head = curl_slist_append(head, "Content-Type:application/x-www-form-u
Windows C下利用wininet傳送http請求
在Windows下,通過使用wininet庫傳送http請求,以下是程式碼例項: char szBuffer[1024] = {0};//宣告全域性變數,作為服務端返回的資料 void SendHttpRequest(char ip[], int port, char
C++ 用libcurl庫進行http 網路通訊程式設計
五、libcurl使用的HTTP訊息頭 當使用libcurl傳送http請求時,它會自動新增一些http頭。我們可以通過CURLOPT_HTTPHEADER屬性手動替換、新增或刪除相應 的HTTP訊息頭。 Host http1.1(大部分http1.0)版本都要求客戶端請求提供這個
C/C++ 用libcurl庫進行http通訊網路程式設計
五、libcurl使用的HTTP訊息頭 當使用libcurl傳送http請求時,它會自動新增一些http頭。我們可以通過CURLOPT_HTTPHEADER屬性手動替換、新增或刪除相應 的HTTP訊息頭。 Host http1.1(大部分http1.0)版本都要求客戶端請求提供這個資
c++使用curl庫傳送https請求
#include "stdafx.h" #include <curl/curl.h> #pragma comment(lib,"libcurl.lib") int _tmain(int argc, _TCHAR* argv[]) { CURL *curl; CURLcode
C++用libcurl庫進行http通訊網路程式設計
//採用CURLOPT_RESUME_FROM_LARGE 實現檔案斷點續傳功能 #include <stdlib.h> #include <stdio.h> #include <sys/stat.h> #include <curl/curl.h> //這個
linux下使用libcurl庫開發http請求客戶端
一、運用開源庫libcurl開發http請求客戶端,實現檔案上傳和字串傳送的功能 /****************************************************** *** Copyright(C) *** author Lu GuoFu *** date 2018-
【python介面自動化】- 使用requests庫傳送http請求
> 前言:什麼是Requests ?Requests 是⽤Python語⾔編寫,基於urllib,採⽤Apache2 Licensed開源協議的 HTTP 庫。它⽐ urllib 更加⽅便,可以節約我們⼤量的⼯作,完全滿⾜HTTP測試需求。 # 安裝requests庫 cmd命令列執行`pip in
C/C++使用libcurl庫發送http請求(get和post可以用於請求html信息,也可以請求xml和json等串)
網絡連接 get 編譯 eas views vs2015 return tar linux C++要實現http網絡連接,需要借助第三方庫,libcurl使用起來還是很方便的 環境:win32 + vs2015 如果要在Linux下使用,基本同理 1,下載
Linux下用c語言實現傳送http請求 方式可以Get或者Post例程參考
[1].[程式碼] Linux下用c語言實現傳送http請求 方式可以Get或者Post 跳至 [1] ? 1 2
Linux下用c語言實現傳送http請求
前言 在linux下,使用socket進行程式設計,需要到伺服器上進行獲取資料,伺服器使用的php程式設計,需要使用http的方式進行獲取資料。 程式碼 #include <stdio.h> #include <string.h&
C傳送http請求
之前用python編寫了傳送http請求的。非常非常方便。很多細節都已經被python內部處理了。這裡來說說C編寫的http請求: #include <stdio.h> #include <string.h> #include <sys/soc
C socket 傳送HTTP請求
HTTP請求頭部樣例: GET http://www.baidu.com/ HTTP/1.1 Accept: html/text Host: 220.181.6.175:80 Connection: Close 這是一個請求百度頁面的頭部。 屬性和值的命名中間用:和空格隔
C++傳送HTTP請求---親測可行(轉)
轉自:http://hi.baidu.com/benbearlove/item/1671c23017575825b3c0c53f環境:xp sp3,vs2008,在靜態庫中使用 MFC#include <afxwin.h>#include <stdio.h&
c# 傳送http請求
/// 傳送請求 /// </summary> /// <param name="url">請求地址</param> /// <param name="sendData"&g
C++ 模擬瀏覽器傳送HTTP請求
#include <afxwin.h> #include <stdio.h> #include <windows.h> #include <string> #include "Wininet.h" #pragma commen
C#後臺傳送HTTP請求
HttpResponse 1.涵蓋POST,GET方法 using System; using System.Collections.Generic; using System.IO; using System.IO.Compression; usi
C#通過GET/POST方式傳送Http請求
介紹http請求的兩種方式,get和post方式。並用C#語言實現,如何請求url並獲取返回的資料 兩者的區別: 引數 Get請求把提交的資料進行簡單編碼,同時將url的一部分發送到伺服器 比如url:Http://127.0.0.1/login.j
C#客戶端傳送http請求與伺服器通訊
本文介紹瞭如何使用C#,通過HttpWebRequest方法,向服務端傳送get,post,put和delete請求 環境介紹 軟體 vs2013 程式語言c# winform 服務端採用java+spring,restful風格 在客戶端,通過H