curl请求HTTP的get和post

#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&amp&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定义部分都注销就好了,有时间再看一下原因。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值