curl請求HTTP的get和post
阿新 • • 發佈:2019-02-02
#include"stdafx.h"
#include <stdio.h>
#include <curl/curl.h>
#include "string"
#include"stdio.h"
#include"stdlib.h"
#include "iostream"
using namespace std;
bool getUrl(char *filename)
{
CURL *curl;
CURLcode res;
FILE *fp;
if ((fp = fopen(filename, "w")) == NULL) // 返回結果用檔案儲存
cout << "create fail";
//system("pause");
struct curl_slist *headers = NULL;
//headers = curl_slist_append(headers, "Accept: Agent-007");
curl = curl_easy_init(); // 初始化
if (curl)
{
curl_easy_setopt(curl, CURLOPT_PROXYAUTH, CURLAUTH_BASIC); //代理認證模式
curl_easy_setopt(curl, CURLOPT_PROXY, "202.112.23.167"); //代理伺服器地址
curl_easy_setopt(curl, CURLOPT_PROXYPORT, 8080); //代理伺服器埠
curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, "yxzhang:zhangyixuan12345"); //http代理認證帳號,名稱:pwd的格式
curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); //使用http代理模式
//curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);// 改協議頭
curl_easy_setopt(curl, CURLOPT_URL, "http://211.65.197.210:8080/IPCIS/activityDatabase/?IpSets=211.65.193.23:32&TableName=2018-06-12&Mode=1");
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); //將返回的http頭輸出到fp指向的檔案
curl_easy_setopt(curl, CURLOPT_HEADERDATA, fp); //將返回的html主體資料輸出到fp指向的檔案
res = curl_easy_perform(curl); // 執行
if (res != 0) {
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
fclose(fp);
return true;
}
}
bool postUrl(char *filename)
{
CURL *curl;
CURLcode res;
FILE *fp;
if ((fp = fopen(filename, "w")) == NULL)
cout << "create fail";
system("pause");
curl = curl_easy_init();
if (curl)
{
struct curl_slist* headerlist = NULL;
// 設定表頭,表頭內容可能不同
headerlist = curl_slist_append(headerlist, "Content-Type:application/x-www-form-urlencoded");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);//改協議頭
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, ""); // 指定cookie檔案
//curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "&prefix_name=42.244.62.215&table_name=2018-06-12"); // 指定post內容
//curl_easy_setopt(curl, CURLOPT_PROXY, "202.112.23.167:8080");
curl_easy_setopt(curl, CURLOPT_PROXYAUTH, CURLAUTH_BASIC); //代理認證模式
curl_easy_setopt(curl, CURLOPT_PROXY, "202.112.23.167"); //代理伺服器地址
curl_easy_setopt(curl, CURLOPT_PROXYPORT, 8080); //代理伺服器埠
curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, "yxzhang:zhangyixuan12345"); //http代理認證帳號,名稱:pwd的格式
curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); //使用http代理模式
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "&username=ipcis&&password=123");
curl_easy_setopt(curl, CURLOPT_URL, "http://ipdb2000.njnet.edu.cn/control_php_ipcis/login.php"); // 指定url
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
fclose(fp);
return true;
}
int main(void)
{
//getUrl("get.html");
string a = "get-ip1.html";
char* aa = (char*)a.data();
getUrl(aa);
system("pause");
}
get執行請求不到,將header定義部分都登出就好了,有時間再看一下原因。
#include <stdio.h>
#include <curl/curl.h>
#include "string"
#include"stdio.h"
#include"stdlib.h"
#include "iostream"
using namespace std;
bool getUrl(char *filename)
{
CURL *curl;
CURLcode res;
FILE *fp;
if ((fp = fopen(filename, "w")) == NULL) // 返回結果用檔案儲存
cout << "create fail";
//system("pause");
struct curl_slist *headers = NULL;
//headers = curl_slist_append(headers, "Accept: Agent-007");
curl = curl_easy_init(); // 初始化
if (curl)
{
curl_easy_setopt(curl, CURLOPT_PROXYAUTH, CURLAUTH_BASIC); //代理認證模式
curl_easy_setopt(curl, CURLOPT_PROXY, "202.112.23.167"); //代理伺服器地址
curl_easy_setopt(curl, CURLOPT_PROXYPORT, 8080); //代理伺服器埠
curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, "yxzhang:zhangyixuan12345"); //http代理認證帳號,名稱:pwd的格式
curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); //使用http代理模式
//curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);// 改協議頭
curl_easy_setopt(curl, CURLOPT_URL, "http://211.65.197.210:8080/IPCIS/activityDatabase/?IpSets=211.65.193.23:32&TableName=2018-06-12&Mode=1");
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); //將返回的http頭輸出到fp指向的檔案
curl_easy_setopt(curl, CURLOPT_HEADERDATA, fp); //將返回的html主體資料輸出到fp指向的檔案
res = curl_easy_perform(curl); // 執行
if (res != 0) {
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
fclose(fp);
return true;
}
}
bool postUrl(char *filename)
{
CURL *curl;
CURLcode res;
FILE *fp;
if ((fp = fopen(filename, "w")) == NULL)
cout << "create fail";
system("pause");
curl = curl_easy_init();
if (curl)
{
struct curl_slist* headerlist = NULL;
// 設定表頭,表頭內容可能不同
headerlist = curl_slist_append(headerlist, "Content-Type:application/x-www-form-urlencoded");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);//改協議頭
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, ""); // 指定cookie檔案
//curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "&prefix_name=42.244.62.215&table_name=2018-06-12"); // 指定post內容
//curl_easy_setopt(curl, CURLOPT_PROXY, "202.112.23.167:8080");
curl_easy_setopt(curl, CURLOPT_PROXYAUTH, CURLAUTH_BASIC); //代理認證模式
curl_easy_setopt(curl, CURLOPT_PROXY, "202.112.23.167"); //代理伺服器地址
curl_easy_setopt(curl, CURLOPT_PROXYPORT, 8080); //代理伺服器埠
curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, "yxzhang:zhangyixuan12345"); //http代理認證帳號,名稱:pwd的格式
curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); //使用http代理模式
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "&username=ipcis&&password=123");
curl_easy_setopt(curl, CURLOPT_URL, "http://ipdb2000.njnet.edu.cn/control_php_ipcis/login.php"); // 指定url
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
fclose(fp);
return true;
}
int main(void)
{
//getUrl("get.html");
string a = "get-ip1.html";
char* aa = (char*)a.data();
getUrl(aa);
system("pause");
}
get執行請求不到,將header定義部分都登出就好了,有時間再看一下原因。