您的位置:首页 > 理论基础 > 计算机网络

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: