前段时间,帮朋友实现linux C实现HTTP get 及POst请求,最原先打算使用libcurl库实现。但是考虑到和其他接口通信的情况,暂时使用C 来实现.代码可以自动解析URL连接,具体看下面代码:
/*File : http.h
*Auth : sjin
*Date : 20141206
*Mail : 413977243@qq.com
*/
#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
/*File : http.c
*Auth : sjin
*Date : 20141206
*Mail : 413977243@qq.com
*/
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
#include "http.h"
#define BUFFER_SIZE 1024
#define HTTP_POST "POST /%s HTTP/1.1rnHOST: %s:%drnAccept: */*rn"
"Content-Type:application/x-www-form-urlencodedrnContent-Length: %drnrn%s"
#define HTTP_GET "GET /%s HTTP/1.1rnHOST: %s:%drnAccept: */*rnrn"
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] = ' ';
if(*(ptr2 + 1)){
memcpy(file,ptr2 + 1,strlen(ptr2) - 1 );
file[strlen(ptr2) - 1] = ' ';
}
}else{
memcpy(host,ptr1,strlen(ptr1));
host[strlen(ptr1)] = ' ';
}
//get host and ip
ptr1 = strchr(host,':');
if(ptr1){
*ptr1++ = ' ';
*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 faindn");
return NULL;
}
if(atoi(ptmp + 9)!=200){
printf("result:n%sn",lpbuf);
return NULL;
}
ptmp = (char*)strstr(lpbuf,"rnrn");
if(!ptmp){
printf("ptmp is NULLn");
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] = {' '};
int socket_fd = -1;
char lpbuf[BUFFER_SIZE*4] = {' '};
char *ptmp;
char host_addr[BUFFER_SIZE] = {' '};
char file[BUFFER_SIZE] = {' '};
int port = 0;
int len=0;
char *response = NULL;
if(!url || !post_str){
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 : %stfile:%st,%dn",host_addr,file,port);
socket_fd = http_tcpclient_create(host_addr,port);
if(socket_fd < 0){
printf("http_tcpclient_create failedn");
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%sn",lpbuf);
/*it's time to recv from server*/
if(http_tcpclient_recv(socket_fd,lpbuf) <= 0){
printf("http_tcpclient_recv failedn");
return NULL;
}
http_tcpclient_close(socket_fd);
return http_parse_result(lpbuf);
}
char * http_get(const char *url)
{
char post[BUFFER_SIZE] = {' '};
int socket_fd = -1;
char lpbuf[BUFFER_SIZE*4] = {' '};
char *ptmp;
char host_addr[BUFFER_SIZE] = {' '};
char file[BUFFER_SIZE] = {' '};
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 : %stfile:%st,%dn",host_addr,file,port);
socket_fd = http_tcpclient_create(host_addr,port);
if(socket_fd < 0){
printf("http_tcpclient_create failedn");
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%sn",lpbuf);
if(http_tcpclient_recv(socket_fd,lpbuf) <= 0){
printf("http_tcpclient_recv failedn");
return NULL;
}
http_tcpclient_close(socket_fd);
return http_parse_result(lpbuf);
} 收藏的用户(0) X
正在加载信息~
推荐阅读
最新回复 (0)
站点信息
- 文章2313
- 用户1336
- 访客11745801
每日一句
Light follows every storm.
风暴后总有光。
风暴后总有光。
jQuery打造漂亮的幻灯片效果
Linux查看进程及相关操作常用命令
解决安卓运行错误Error:Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug
华为:你硬要打压,我偏要强大!
thinkpad t470p装黑苹果系统10.13.2
Android常用的数学函数说明
Android Studio使用DB Browser查看SQLite数据库
使用Putty上传文件?
关于IDEA的Spring boot项目创建慢,Maven插件加载慢,依赖导入慢或者失败的原因及解决方案
imencode和imdecode使用
element-UI组件实现拖拽效果
【教程】手把手教你开通淘小铺赚佣金
Android简单树状实现
新会员