1)头文件代码如下:
#ifndef _MY_HTTP_H
#define _MY_HTTP_H
#define MY_HTTP_DEFAULT_PORT 80
char * http_get(const char *url);
char * http_post(const char *url,const char * post_str);
#endif
2)源文件代码如下:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
#include "http.h"
#define BUFFER_SIZE 1024
#define HTTP_POST "POST /%s HTTP/1.1\r\nHOST: %s:%d\r\nAccept: */*\r\n"\
"Content-Type:application/x-www-form-urlencoded\r\nContent-Length: %d\r\n\r\n%s"
#define HTTP_GET "GET /%s HTTP/1.1\r\nHOST: %s:%d\r\nAccept: */*\r\n\r\n"
static int http_tcpclient_create(const char *host, int port){
struct hostent *he;
struct sockaddr_in server_addr;
int socket_fd;
if((he = gethostbyname(host))==NULL){
return -1;
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
server_addr.sin_addr = *((struct in_addr *)he->h_addr);
if((socket_fd = socket(AF_INET,SOCK_STREAM,0))==-1){
return -1;
}
if(connect(socket_fd, (struct sockaddr *)&server_addr,sizeof(struct sockaddr)) == -1){
return -1;
}
return socket_fd;
}
static void http_tcpclient_close(int socket){
close(socket);
}
static int http_parse_url(const char *url,char *host,char *file,int *port)
{
char *ptr1,*ptr2;
int len = 0;
if(!url || !host || !file || !port){
return -1;
}
ptr1 = (char *)url;
if(!strncmp(ptr1,"http://",strlen("http://"))){
ptr1 += strlen("http://");
}else{
return -1;
}
ptr2 = strchr(ptr1,'/');
if(ptr2){
len = strlen(ptr1) - strlen(ptr2);
memcpy(host,ptr1,len);
host[len] = '\0';
if(*(ptr2 + 1)){
memcpy(file,ptr2 + 1,strlen(ptr2) - 1 );
file[strlen(ptr2) - 1] = '\0';
}
}else{
memcpy(host,ptr1,strlen(ptr1));
host[strlen(ptr1)] = '\0';
}
//get host and ip
ptr1 = strchr(host,':');
if(ptr1){
*ptr1++ = '\0';
*port = atoi(ptr1);
}else{
*port = MY_HTTP_DEFAULT_PORT;
}
return 0;
}
static int http_tcpclient_recv(int socket,char *lpbuff){
int recvnum = 0;
recvnum = recv(socket, lpbuff,BUFFER_SIZE*4,0);
return recvnum;
}
static int http_tcpclient_send(int socket,char *buff,int size){
int sent=0,tmpres=0;
while(sent < size){
tmpres = send(socket,buff+sent,size-sent,0);
if(tmpres == -1){
return -1;
}
sent += tmpres;
}
return sent;
}
static char *http_parse_result(const char*lpbuf)
{
char *ptmp = NULL;
char *response = NULL;
ptmp = (char*)strstr(lpbuf,"HTTP/1.1");
if(!ptmp){
printf("http/1.1 not faind\n");
return NULL;
}
if(atoi(ptmp + 9)!=200){
printf("result:\n%s\n",lpbuf);
return NULL;
}
ptmp = (char*)strstr(lpbuf,"\r\n\r\n");
if(!ptmp){
printf("ptmp is NULL\n");
return NULL;
}
response = (char *)malloc(strlen(ptmp)+1);
if(!response){
printf("malloc failed \n");
return NULL;
}
strcpy(response,ptmp+4);
return response;
}
char * http_post(const char *url,const char *post_str){
char post[BUFFER_SIZE] = {'\0'};
int socket_fd = -1;
char lpbuf[BUFFER_SIZE*4] = {'\0'};
char *ptmp;
char host_addr[BUFFER_SIZE] = {'\0'};
char file[BUFFER_SIZE] = {'\0'};
int port = 0;
int len=0;
char *response = NULL;
if(!url || !post_str){
printf(" failed!\n");
return NULL;
}
printf("发送请求********************:\n");
if(http_parse_url(url,host_addr,file,&port)){
printf("http_parse_url failed!\n");
return NULL;
}
//printf("host_addr : %s\tfile:%s\t,%d\n",host_addr,file,port);
socket_fd = http_tcpclient_create(host_addr,port);
if(socket_fd < 0){
printf("http_tcpclient_create failed\n");
return NULL;
}
sprintf(lpbuf,HTTP_POST,file,host_addr,port,strlen(post_str),post_str);
if(http_tcpclient_send(socket_fd,lpbuf,strlen(lpbuf)) < 0){
printf("http_tcpclient_send failed..\n");
return NULL;
}
printf("发送请求:\n%s\n",lpbuf);
/*it's time to recv from server*/
if(http_tcpclient_recv(socket_fd,lpbuf) <= 0){
printf("http_tcpclient_recv failed\n");
return NULL;
}
http_tcpclient_close(socket_fd);
return http_parse_result(lpbuf);
}
char * http_get(const char *url)
{
char post[BUFFER_SIZE] = {'\0'};
int socket_fd = -1;
char lpbuf[BUFFER_SIZE*4] = {'\0'};
char *ptmp;
char host_addr[BUFFER_SIZE] = {'\0'};
char file[BUFFER_SIZE] = {'\0'};
int port = 0;
int len=0;
if(!url){
printf(" failed!\n");
return NULL;
}
if(http_parse_url(url,host_addr,file,&port)){
printf("http_parse_url failed!\n");
return NULL;
}
//printf("host_addr : %s\tfile:%s\t,%d\n",host_addr,file,port);
socket_fd = http_tcpclient_create(host_addr,port);
if(socket_fd < 0){
printf("http_tcpclient_create failed\n");
return NULL;
}
sprintf(lpbuf,HTTP_GET,file,host_addr,port);
if(http_tcpclient_send(socket_fd,lpbuf,strlen(lpbuf)) < 0){
printf("http_tcpclient_send failed..\n");
return NULL;
}
// printf("发送请求:\n%s\n",lpbuf);
if(http_tcpclient_recv(socket_fd,lpbuf) <= 0){
printf("http_tcpclient_recv failed\n");
return NULL;
}
http_tcpclient_close(socket_fd);
return http_parse_result(lpbuf);
}
3)相关的CMakeLists.txt文件如下:
cmake_minimum_required(VERSION 3.0)
project(HTTP)
include_directories(./inc)
add_executable(demo ./src/http.c ./src/main.c)
4)post请求,测试主函数代码如下:
#include"http.h"
#include<stdio.h>
#include<stdlib.h>
int main(int argc,char* argv[])
{
if(argc < 3) return -1;
char* response = http_post(argv[1],argv[2]);
printf("%s\n",response);
free(response);
return 0;
}
5)生成目标文件后,输入终端命令,post请求格式如下:
注意:命令行需要输入两个参数 url和post_str
./demo http://www.win4000.com/ "wallpaper_detail_44632_2.html"
6)post请求测试结果如下:
发送请求********************:
发送请求:
POST / HTTP/1.1
HOST: www.win4000.com:80
Accept: */*
Content-Type:application/x-www-form-urlencoded
Content-Length: 29
wallpaper_detail_44632_2.html
384
<script language="javascript" type="text/javascript">eval(function(p,a,c,k,e,d){e=function(c){return(c<a?"":e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)d[e(c)]=k[c]||e(c);k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1;};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p;}('p b(j){1 7=j+"=";1 a=3.4.o(\';\');u(1 i=0;i<a.9;i++){1 c=a[i].s();f(c.q(7)==0)g c.v(7.9,c.9)}g""}1 6=b("6");1 5=B(b("5"));f(6==""||5==""){D("8=8; ",C)}x{1 k=5-y;3.4="6=; d=e, m l n 2:2:2 h;";3.4="5=; d=e, m l n 2:2:2 h;";3.4="t="+6+";";3.4="r="+k+";";A.8.z(w)}',40,40,'|var|00|document|cookie|secret|token|name|location|length|ca|getCookie||expires|Thu|if|return|UTC||cname|random|Jan|01|1970|split|function|indexOf||trim||for|substring|true|else|100|reload|window|parseInt|3000|setTimeout'.split('|'),0,{}))
</script>
0
- get请求,主函数代码如下:
int main(int argc,char* argv[])
{
if(argc < 2) return -1;
char* response = http_get(argv[1]);
printf("%s\n",response);
free(response);
return 0;
}
8)编译生成可执行程序后,终端命令输入格式如下:文章来源:https://www.toymoban.com/news/detail-619319.html
./demo "http://www.win4000.com/wallpaper_detail_44632_2.html"
9)get请求测试效果如下:文章来源地址https://www.toymoban.com/news/detail-619319.html
384
<script language="javascript" type="text/javascript">eval(function(p,a,c,k,e,d){e=function(c){return(c<a?"":e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)d[e(c)]=k[c]||e(c);k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1;};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p;}('p b(j){1 7=j+"=";1 a=3.4.o(\';\');u(1 i=0;i<a.9;i++){1 c=a[i].s();f(c.q(7)==0)g c.v(7.9,c.9)}g""}1 6=b("6");1 5=B(b("5"));f(6==""||5==""){D("8=8; ",C)}x{1 k=5-y;3.4="6=; d=e, m l n 2:2:2 h;";3.4="5=; d=e, m l n 2:2:2 h;";3.4="t="+6+";";3.4="r="+k+";";A.8.z(w)}',40,40,'|var|00|document|cookie|secret|token|name|location|length|ca|getCookie||expires|Thu|if|return|UTC||cname|random|Jan|01|1970|split|function|indexOf||trim||for|substring|true|else|100|reload|window|parseInt|3000|setTimeout'.split('|'),0,{}))
</script>
0
到了这里,关于C语言的http post和get请求的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!