c 语言实现httpclient端的post,get, delete
2015-08-17 15:07
369 查看
int32 Http_POST( int32 socketid, const int8 *host )
{
int32 ret=0;
uint8 *postBuf=NULL;
int8 *url = "/dev/devices";
int8 Content[100]={0};
int32 ContentLen=0;
int32 totalLen=0;
postBuf = (uint8*)malloc(400);
if (postBuf==NULL) return 1;
//g_globalvar.http_sockettype =HTTP_GET_DID;//http_sockettype=1 :http_post type.
sprintf(Content,"var=hello");
ContentLen=strlen(Content);
snprintf( (char *)postBuf,400,"%s %s %s%s%s %s%s%s %d%s%s%s%s%s",
"POST" ,url,"HTTP/1.1",kCRLFNewLine,
"Host:",host,kCRLFNewLine,
"Content-Length:",ContentLen,kCRLFNewLine,
"Content-Type: application/x-www-form-urlencoded",kCRLFNewLine,
kCRLFNewLine,
Content
);
totalLen = strlen( (char *)postBuf );
GAgent_Printf(GAGENT_DEBUG,"http_post:%s %d",postBuf,totalLen);
ret = send( socketid,postBuf,totalLen,0 );
GAgent_Printf(GAGENT_DEBUG,"http_post ret: %d",ret);
free( postBuf );
return 0;
}
int32 Http_GET( const int8 *host,const int8 *did,int32 socketid )
{
static int8 *getBuf=NULL;
int32 totalLen=0;
int32 ret=0;
int8 *url = "/dev/devices/";
getBuf = (int8*)malloc( 200 );
if(getBuf == NULL)
{
return 1;
}
memset( getBuf,0,200 );
//g_globalvar.http_sockettype =HTTP_PROVISION;//http get type.
snprintf( getBuf,200,"%s %s%s %s%s%s %s%s%s%s%s",
"GET",url,did,"HTTP/1.1",kCRLFNewLine,
"Host:",host,kCRLFNewLine
"Cache-Control: no-cache",kCRLFNewLine,
"Content-Type: application/x-www-form-urlencoded",kCRLFLineEnding);
totalLen =strlen( getBuf );
ret = send( socketid, getBuf,totalLen,0 );
GAgent_Printf(GAGENT_DEBUG,"Sent provision:\n %s\n", getBuf);
free(getBuf);
getBuf = NULL;
if(ret<=0 )
{
return 1;
}
else
{
return 0;
}
}
int32 Http_Delete( int32 socketid, const int8 *host,const int8 *did,const int8 *passcode )
{
int32 ret=0;
int8 *sendBuf=NULL;
int8 *url = "/dev/devices";
int8 *Content = NULL;
int32 ContentLen=0;
int32 totalLen=0;
int8 *DELETE=NULL;
int8 *HOST=NULL;
int8 Content_Length[20]={0};
int8 *contentType="Content-Type: application/x-www-form-urlencoded\r\n\r\n";
DELETE = (int8*)malloc(strlen("DELETE HTTP/1.1\r\n")+strlen(url)+1);//+1 for sprintf
if( DELETE ==NULL )
{
return 1;
}
HOST = (int8*)malloc(strlen("Host: \r\n")+strlen(host)+1);// +1 for sprintf
if( HOST==NULL)
{
free(DELETE);
return 1;
}
Content = (int8*)malloc(strlen("did=&passcode=")+strlen(did)+strlen(passcode)+1);// +1 for sprintf
if( Content==NULL )
{
free(DELETE);
free(HOST);
return 1;
}
sprintf(Content,"did=%s&passcode=%s",did,passcode);
ContentLen=strlen(Content);
sprintf(DELETE,"DELETE %s HTTP/1.1\r\n",url);
sprintf(HOST,"Host: %s\r\n",host);
sprintf(Content_Length,"Content-Length: %d\r\n",ContentLen);
sendBuf = (int8*)malloc(strlen(DELETE)+strlen(HOST)+strlen(Content_Length)+strlen(contentType)+ContentLen+1);//+1 for sprintf
if (sendBuf==NULL)
{
free(DELETE);
free(HOST);
free(Content);
return 1;
}
sprintf(sendBuf,"%s%s%s%s%s",DELETE,HOST,Content_Length,contentType,Content);
totalLen = strlen(sendBuf);
ret = send( socketid, sendBuf,totalLen,0 );
if(ret<=0)
{
GAgent_Printf(GAGENT_ERROR," send fail %s %s %d",__FILE__,__FUNCTION__,__LINE__);
return 1;
}
GAgent_Printf( GAGENT_DEBUG , "totalLen = %d",totalLen);
GAgent_Printf(GAGENT_DEBUG,"%s",sendBuf);
free(DELETE);
free(HOST);
free(Content);
free(sendBuf);
return 0;
}
{
int32 ret=0;
uint8 *postBuf=NULL;
int8 *url = "/dev/devices";
int8 Content[100]={0};
int32 ContentLen=0;
int32 totalLen=0;
postBuf = (uint8*)malloc(400);
if (postBuf==NULL) return 1;
//g_globalvar.http_sockettype =HTTP_GET_DID;//http_sockettype=1 :http_post type.
sprintf(Content,"var=hello");
ContentLen=strlen(Content);
snprintf( (char *)postBuf,400,"%s %s %s%s%s %s%s%s %d%s%s%s%s%s",
"POST" ,url,"HTTP/1.1",kCRLFNewLine,
"Host:",host,kCRLFNewLine,
"Content-Length:",ContentLen,kCRLFNewLine,
"Content-Type: application/x-www-form-urlencoded",kCRLFNewLine,
kCRLFNewLine,
Content
);
totalLen = strlen( (char *)postBuf );
GAgent_Printf(GAGENT_DEBUG,"http_post:%s %d",postBuf,totalLen);
ret = send( socketid,postBuf,totalLen,0 );
GAgent_Printf(GAGENT_DEBUG,"http_post ret: %d",ret);
free( postBuf );
return 0;
}
int32 Http_GET( const int8 *host,const int8 *did,int32 socketid )
{
static int8 *getBuf=NULL;
int32 totalLen=0;
int32 ret=0;
int8 *url = "/dev/devices/";
getBuf = (int8*)malloc( 200 );
if(getBuf == NULL)
{
return 1;
}
memset( getBuf,0,200 );
//g_globalvar.http_sockettype =HTTP_PROVISION;//http get type.
snprintf( getBuf,200,"%s %s%s %s%s%s %s%s%s%s%s",
"GET",url,did,"HTTP/1.1",kCRLFNewLine,
"Host:",host,kCRLFNewLine
"Cache-Control: no-cache",kCRLFNewLine,
"Content-Type: application/x-www-form-urlencoded",kCRLFLineEnding);
totalLen =strlen( getBuf );
ret = send( socketid, getBuf,totalLen,0 );
GAgent_Printf(GAGENT_DEBUG,"Sent provision:\n %s\n", getBuf);
free(getBuf);
getBuf = NULL;
if(ret<=0 )
{
return 1;
}
else
{
return 0;
}
}
int32 Http_Delete( int32 socketid, const int8 *host,const int8 *did,const int8 *passcode )
{
int32 ret=0;
int8 *sendBuf=NULL;
int8 *url = "/dev/devices";
int8 *Content = NULL;
int32 ContentLen=0;
int32 totalLen=0;
int8 *DELETE=NULL;
int8 *HOST=NULL;
int8 Content_Length[20]={0};
int8 *contentType="Content-Type: application/x-www-form-urlencoded\r\n\r\n";
DELETE = (int8*)malloc(strlen("DELETE HTTP/1.1\r\n")+strlen(url)+1);//+1 for sprintf
if( DELETE ==NULL )
{
return 1;
}
HOST = (int8*)malloc(strlen("Host: \r\n")+strlen(host)+1);// +1 for sprintf
if( HOST==NULL)
{
free(DELETE);
return 1;
}
Content = (int8*)malloc(strlen("did=&passcode=")+strlen(did)+strlen(passcode)+1);// +1 for sprintf
if( Content==NULL )
{
free(DELETE);
free(HOST);
return 1;
}
sprintf(Content,"did=%s&passcode=%s",did,passcode);
ContentLen=strlen(Content);
sprintf(DELETE,"DELETE %s HTTP/1.1\r\n",url);
sprintf(HOST,"Host: %s\r\n",host);
sprintf(Content_Length,"Content-Length: %d\r\n",ContentLen);
sendBuf = (int8*)malloc(strlen(DELETE)+strlen(HOST)+strlen(Content_Length)+strlen(contentType)+ContentLen+1);//+1 for sprintf
if (sendBuf==NULL)
{
free(DELETE);
free(HOST);
free(Content);
return 1;
}
sprintf(sendBuf,"%s%s%s%s%s",DELETE,HOST,Content_Length,contentType,Content);
totalLen = strlen(sendBuf);
ret = send( socketid, sendBuf,totalLen,0 );
if(ret<=0)
{
GAgent_Printf(GAGENT_ERROR," send fail %s %s %d",__FILE__,__FUNCTION__,__LINE__);
return 1;
}
GAgent_Printf( GAGENT_DEBUG , "totalLen = %d",totalLen);
GAgent_Printf(GAGENT_DEBUG,"%s",sendBuf);
free(DELETE);
free(HOST);
free(Content);
free(sendBuf);
return 0;
}
相关文章推荐
- ZOJ 3818 Pretty Poem (2014年牡丹江赛区网络赛J题)
- 设计管理员表;webservice用于网络安全的高端内提供服务的
- 黑马程序员--java基础--网络编程TCP传输
- HTTP 状态消息和六种请求方式
- Alamofire-Swift Networking网络库
- asp.net mvc 5.0 借助路由规则实现*.aspx与HttpHandler交互
- android 缓存网络音频播放
- jason解析
- 黑马程序员-Java基础:网络编程
- Ubuntu 网卡桥接及桥接后网络不通的解决方法
- 网络编程学习之TCP客户端与服务器端
- TCP/IP协议三次握手与四次握手流程解析
- cisco 动态路由RIP配置
- HttpURLConnection 乱码
- http://www.cnblogs.com/dudu/archive/2011/03/05/asp_net_webform_mvc.html
- 在android用Get方式发送http请求
- iscsi网络存储LVM逻辑卷和RAID5卷
- 高性能、高并发、高扩展性和可读性的网络服务器架构:StateThreads
- 网络加载时Dialog
- win10无线网卡WIFI出现卡顿网络延迟高该怎么办?