C语言发送post请求数据程序

C语言发送post请求数据程序,第1张

C语言发post请求数据程序, 工作需要,网上查资料N篇,作为半路出家学编程的,走过了N个坑,终于完成以下的测试程序。 使用了curl的库, 这样无论在windows或者在linux都可以使用. win下的编程环境是TDM-GCC-64, 怎样安装,也是另一个话题。需要这个的请自行上网查询怎样安装. linux 下是gcc环境,最好先安装curl开发包,目的就是需要curl.h等文件, 怎样安装,也是另一个话题。 废话不说,以下是正式程序. #include #include #include #include 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 ") exit(EXIT_FAILURE) } s->ptr[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 ") exit(EXIT_FAILURE) } memcpy(s->ptr + s->len, ptr, size*nmemb) s->ptr[new_len] = '' s->len = new_lenreturn 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=>=>", s.ptr) sprintf(response," =>%s ", 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... ") struct curl_slist *headers1 = NULL headers1 = curl_slist_append(headers1, "User-Agent: Mozilla/5.0 (Windows NT 10.0WOW64Trident/7.0rv:11.0) like Gecko")headers1 = curl_slist_append(headers1, "Content-Type:application/x-www-form-urlencodedcharset=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... ") 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

OK

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/ip.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#include <stdio.h>

#include <stdlib.h>

#include <strings.h>

#include <unistd.h>

#include <string.h>

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

{

int sockfd,new_socket

int sock_value

char buf[] = "hello! China!I Love You\n"

struct sockaddr_in client_

struct sockaddr_in server_

int SIZE = sizeof(struct sockaddr_in)

if(argc != 2){

fprintf(stderr,"The two number!\n")

exit(1)

}

if((sock_value = atoi(argv[1])) <0){

fprintf(stderr,"socket error!\n")

exit(1)

}

if((sockfd = socket(PF_INET,SOCK_STREAM, 0)) == -1){

perror("socket")

exit(1)

}

bzero(&server_,SIZE)

server_.sin_family = PF_INET

server_.sin_port = htons(sock_value)

server_.sin_addr.s_addr = INADDR_ANY

if(bind(sockfd,(struct sockaddr *)(&server_),SIZE) == -1){

perror("bind")

exit(1)

}

if(listen(sockfd, 12) == -1){

perror("listen")

exit(1)

}

printf("Waiting ... ...\n")

while(1){

if((new_socket = accept(sockfd,(struct sockaddr *)(&client_),&SIZE)) == -1){

perror("accept")

exit(1)

}

printf("The client IP is %s\n",inet_ntoa(client_.sin_addr))

printf("The socket is %d\n",ntohs(client_.sin_port))

if(write(new_socket,buf,strlen(buf)) == -1){

perror("write")

exit(1)

}

int my

char mybuf[1024]

if((my = read(new_socket, mybuf,1024)) == -1){

perror("read")

exit(1)

}

mybuf[my] = '\0'

printf("#++++#++++#:%s\n",mybuf)

close(new_socket)

}

close(sockfd)

return 0

}

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/ip.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#include <stdio.h>

#include <stdlib.h>

#include <strings.h>

#include <unistd.h>

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

{

int sockfd

int sock_value

char buf[1024]

char mybuf[] = "Linux\n"

int read_count

struct sockaddr_in client_

struct sockaddr_in server_

int SIZE = sizeof(struct sockaddr_in)

if(argc != 3){

fprintf(stderr,"The two number!\n")

exit(1)

}

if((sock_value = atoi(argv[2])) <0){

fprintf(stderr,"socket error!\n")

exit(1)

}

if((sockfd = socket(PF_INET,SOCK_STREAM, 0)) == -1){

perror("socket")

exit(1)

}

bzero(&client_,SIZE)

bzero(&server_,SIZE)

client_.sin_family = PF_INET

client_.sin_port = htons(52252)

client_.sin_addr.s_addr = INADDR_ANY

server_.sin_family = PF_INET

server_.sin_port = htons(sock_value)

server_.sin_addr.s_addr = inet_addr(argv[1])

if(connect(sockfd,(struct sockaddr *)(&server_),SIZE) == -1){

perror("connect")

exit(1)

}

if((read_count = read(sockfd,buf,1024)) == -1){

perror("read")

exit(1)

}

buf[read_count] = '\0'

printf("#----#----#:%s\n",buf)

if(write(sockfd, mybuf,6) == -1){

perror("write")

exit(1)

}

close(sockfd)

exit(0)

return 0

}

以下方法用CURL提交post表单

1. 编译环境.

安装vs2010或其他版本. vs2010 express版也可以。不要低于vc6.

2. 搜索curl-7.25.0.zip,下载。

解压到c:\curl-7.25.0

打开Visual Studio Command Prompt (2010)

cd \curl-7.25.0\winbuild

nmake /f Makefile.vc mode=dll USE_SSSPI=no ENABLE_IDN=no

编译成功后 cd ..\builds

到一个名字为libcurl-....lib的子目录里找到libcurl.dll和libcurl.lib, 保存到一个目录下备份,下面要用。

3. 打开vc++ 2010, File->New project,选Win32 Project, 输入一个项目名。下面点Next,勾上Console Application和Empty Project.

4. 配置项目

到我的文档下找到vs2010 projects目录,找到 solution名字\项目名字 目录,

把curl-7.25.0目录下的include目录拷贝到项目目录下

把2备份好的libcurl.dll和libcurl.lib拷贝到项目目录.

在vc++中右键点击项目名(或Alt+F7), 点开Configuration Properties, 点vc++directories

点Include Directories, 点Edit, 添加$(ProjectDir)include 确定

在点击左侧的Linker, 点Input,点Additional Dependences, 点Edit, 添加一行$(ProjectDir)\libcurl.lib 确定

5. 代码。

右键点项目名字,Add New Item->C++ File, name写main.c, 输入代码:

/* 抱歉,这里不好贴链接,版权没法贴,版权去看http-post.c */

#include <stdio.h>

#include <curl/curl.h>

#include <stdlib.h>

int main(void)

{

CURL *curl

CURLcode res

curl = curl_easy_init()

if(curl) {

/* First set the URL that is about to receive our POST. This URL can

just as well be a https:// URL if that is what should receive the

data. */

curl_easy_setopt(curl, CURLOPT_URL, "这里写网址")

/* Now specify the POST data */

curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "name=daniel&project=curl")

/* Perform the request, res will get the return code */

res = curl_easy_perform(curl)

/* always cleanup */

curl_easy_cleanup(curl)

system("pause")

}

return 0

}

点vc++绿色的三角编译运行。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/7521070.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-06
下一篇 2023-04-06

发表评论

登录后才能评论

评论列表(0条)

保存