首页 » SEO优化 » php吸收delphipost技巧_C措辞发送post请求数据轨范

php吸收delphipost技巧_C措辞发送post请求数据轨范

访客 2024-12-19 0

扫一扫用手机浏览

文章目录 [+]

利用了curl的库, 这样无论在windows或者在linux都可以利用.

win下的编程环境是TDM-GCC-64, 若何安装,也是另一个话题。
须要这个的请自行上网查询若何安装.

php吸收delphipost技巧_C措辞发送post请求数据轨范

linux 下是gcc环境,最好先安装curl开拓包,目的便是须要curl.h等文件, 若何安装,也是另一个话题。

php吸收delphipost技巧_C措辞发送post请求数据轨范
(图片来自网络侵删)

废话不说,以下是正式程序.

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <curl/curl.h>

struct string {

char ptr;

size_t len;

};

void init_string(struct string s) {

s->len = 0;

s->ptr = malloc(s->len + 1);

if (s->ptr == NULL) {

fprintf(stderr, "malloc() failed\n");

exit(EXIT_FAILURE);

}

s->ptr[0] = '\0';

}

size_t writefunc(void ptr, size_t size, size_t nmemb, struct string s)

{

size_t new_len = s->len + size nmemb;

s->ptr = realloc(s->ptr, new_len + 1);

if (s->ptr == NULL) {

fprintf(stderr, "realloc() failed\n");

exit(EXIT_FAILURE);

}

memcpy(s->ptr + s->len, ptr, sizenmemb);

s->ptr[new_len] = '\0';

s->len = new_len;

return size nmemb;

}

CURLcode curl_post_req(char url, char postParams,struct curl_slist headers, char response)

{

CURL curl;

curl = curl_easy_init(); //初始化

// curl返回值

CURLcode res;

if (curl)

{

struct string s;

init_string(&s);

curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

//curl_easy_setopt(curl, CURLOPT_URL, "http://httpbin.org/post");

curl_easy_setopt(curl, CURLOPT_URL, url);

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);

curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);

curl_easy_setopt(curl, CURLOPT_POST, 1);//设置CURLOPT_POST之后必须带有POST数据

curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postParams);

//不吸收相应头数据0代表不吸收 1代表吸收

curl_easy_setopt(curl, CURLOPT_HEADER, 0);

curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

//CURLOPT_VERBOSE的值为1时,会显示详细的调试信息

curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);

curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);

curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);

//设置超时时间,以秒来打算 CURLOPT_CONNECTTIMEOUT是连接超时

curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10);

curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);

// https ssl 时须要用到,如果是 http 可以注释掉

curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);

curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);

res = curl_easy_perform(curl);

//printf(" =>%s=>=>\n", s.ptr);

sprintf(response," =>%s\n", s.ptr);

free(s.ptr);

curl_slist_free_all(headers);

};

curl_easy_cleanup(curl);

return res;

}

int main(int argc, const char argv[])

{

// http 要求头, 布局

printf(" start...\n");

struct curl_slist headers1 = NULL;

headers1 = curl_slist_append(headers1, "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko");

headers1 = curl_slist_append(headers1, "Content-Type:application/x-www-form-urlencoded; charset=UTF-8");

char url_post0[100] = "https://www.lpfrx.com/";

// 查找的字符串 : delphi

char paramsLogin0[100] = "s=delphi";

char resPost0[40960] = "";

CURLcode res3 = curl_post_req(url_post0, paramsLogin0, headers1,resPost0);

if (res3 == CURLE_OK)

{

printf("data: %s" ,resPost0);

}

printf(" end...\n");

return 0;

}

// win 下cmd下运行乱码,请先实行 chcp 65001 转成 utf8. 默认是chcp 936

// win: 运行目录下须要zlib.dll libcurl.dll

// curl-Library 是我自己的目录,放在当前的程序目录下,win下的curl.h 也是须要自己去找,如果有python编程环境的话,也安装了curl库的话,该当可能会有curl.h的库路径

// win: gcc -o curlpostlpfrx curlpost_lpfrx.c -I ./curl-Library/include -L ./curl-Library/lib -lcurl

//linux: gcc -o curlpostlpfrx curlpost_lpfrx.c -lcurl

标签:

相关文章