// Request.cpp: implementation of the Request class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "HTTPrequest.h"
#include "Request.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Request::Request()
{
}
Request::~Request()
{
}
//*******************************************************************************************************
//MemBufferCreate:
// Passed a MemBuffer structure, will allocate a memory buffer
// of MEM_BUFFER_SIZE. This buffer can then grow as needed.
//*******************************************************************************************************
void Request::MemBufferCreate(MemBuffer *b)
{
b->size = MEM_BUFFER_SIZE;
b->buffer =(unsigned char *) malloc( b->size );
b->position = b->buffer;
}
//*******************************************************************************************************
// MemBufferGrow:
// Double the size of the buffer that was passed to this function.
//*******************************************************************************************************
void Request::MemBufferGrow(MemBuffer *b)
{
size_t sz;
sz = b->position - b->buffer;
b->size = b->size *2;
b->buffer =(unsigned char *) realloc(b->buffer,b->size);
b->position = b->buffer + sz; // readjust current position
}
//*******************************************************************************************************
// MemBufferAddByte:
// Add a single byte to the memory buffer, grow if needed.
//*******************************************************************************************************
void Request::MemBufferAddByte(MemBuffer *b,unsigned char byt)
{
if( (size_t)(b->position-b->buffer) >= b->size )
MemBufferGrow(b);
*(b->position++) = byt;
}
//*******************************************************************************************************
// MemBufferAddBuffer:
// Add a range of bytes to the memory buffer, grow if needed.
//*******************************************************************************************************
void Request::MemBufferAddBuffer(MemBuffer *b,
unsigned char *buffer, size_t size)
{
while( ((size_t)(b->position-b->buffer)+size) >= b->size )
MemBufferGrow(b);
memcpy(b->position,buffer,size);
b->position+=size;
}
//*******************************************************************************************************
// GetHostAddress:
// Resolve using DNS or similar(WINS,etc) the IP
// address for a domain name such as www.wdj.com.
//*******************************************************************************************************
DWORD Request::GetHostAddress(LPCSTR host)
{
struct hostent *phe;
char *p;
phe = gethostbyname( host );
if(phe==NULL)
return 0;
p = *phe->h_addr_list;
return *((DWORD*)p);
}
//*******************************************************************************************************
// SendString:
// Send a string(null terminated) over the specified socket.
//*******************************************************************************************************
void Request::SendString(SOCKET sock,LPCSTR str)
{
send(sock,str,strlen(str),0);
}
//*******************************************************************************************************
// ValidHostChar:
// Return TRUE if the specified character is valid
// for a host name, i.e. A-Z or 0-9 or -.:
//*******************************************************************************************************
BOOL Request::ValidHostChar(char ch)
{
return( isalpha(ch) || isdigit(ch)
|| ch=='-' || ch=='.' || ch==':' );
}
//*******************************************************************************************************
// ParseURL:
// Used to break apart a URL such as
// http://www.localhost.com:80/TestPost.htm into protocol, port, host and request.
//*******************************************************************************************************
void Request::ParseURL(LPCSTR url,LPSTR protocol,int lprotocol,
LPSTR host,int lhost,LPSTR request,int lrequest,int *port)
{
char *work,*ptr,*ptr2;
*protocol = *host = *request = 0;
*port=80;
work = strdup(url);
strupr(work);
ptr = strchr(work,':'); // find protocol if any
if(ptr!=NULL)
{
*(ptr++) = 0;
lstrcpyn(protocol,work,lprotocol);
}
else
{
lstrcpyn(protocol,"HTTP",lprotocol);
ptr = work;
}
if( (*ptr=='/') && (*(ptr+1)=='/') ) // skip past opening /'s
ptr+=2;
ptr2 = ptr; // find host
while( ValidHostChar(*ptr2) && *ptr2 )
ptr2++;
*ptr2=0;
lstrcpyn(host,ptr,lhost);
lstrcpyn(request,url + (ptr2-work),lrequest); // find the request
ptr = strchr(host,':'); // find the port number, if any
if(ptr!=NULL)
{
*ptr=0;
*port = atoi(ptr+1);
}
free(work);
}
//*******************************************************************************************************
// SendHTTP:
// Main entry point for this code.
// url - The URL to GET/POST to/from.
// headerSend - Headers to be sent to the server.
// post - Data to be posted to the server, NULL if GET.
// postLength - Length of data to post.
// req - Contains the message and headerSend sent by the server.
//
// returns 1 on failure, 0 on success.
//*******************************************************************************************************
int Request::SendHTTP(LPCSTR url,LPCSTR headerReceive,BYTE *post,
DWORD postLength,HTTPRequest *req)
{
WSADATA WsaData;
SOCKADDR_IN sin;
SOCKET sock;
char buffer[512];
char protocol[20],host[256],request[1024];
int l,port,chars,err;
MemBuffer headersBuffer,messageBuffer;
char headerSend[1024];
BOOL done;
ParseURL(url,protocol,sizeof(protocol),host,sizeof(host), // Parse the URL
request,sizeof(request),&port);
if(strcmp(protocol,"HTTP"))
return 1;
err = WSAStartup (0x0101, &WsaData); // Init Winsock
if(err!=0)
return 1;
sock = socket (AF_INET, SOCK_STREAM, 0);
//if (socket == INVALID_SOCKET)
if (sock == INVALID_SOCKET)
return 1;
sin.sin_family = AF_INET; //Connect to web sever
sin.sin_port = htons( (unsigned short)port );
sin.sin_addr.s_addr = GetHostAddress(host);
if( connect (sock,(LPSOCKADDR)&sin, sizeof(SOCKADDR_IN) ) )
{
return 1;
}
//printf("\r\n\r\n <<SEND HTTP REQUEST : >> \r\n\r\n"); //Send request
if( !*request )
lstrcpyn(request,"/",sizeof(request));
if( post == NULL )
{
SendString(sock,"GET ");
strcpy(headerSend, "GET ");
}
else
{
SendString(sock,"POST ");
strcpy(headerSend, "POST ");
}
SendString(sock,request);
strcat(headerSend, request);
SendString(sock," HTTP/1.0\r\n");
strcat(headerSend, " HTTP/1.0\r\n");
SendString(sock,"Accept: image/gif, image/x-xbitmap,"
" image/jpeg, image/pjpeg, application/vnd.ms-excel,"
" application/msword, application/vnd.ms-powerpoint,"
" */*\r\n");
strcat(headerSend, "Accept: image/gif, image/x-xbitmap,"
" image/jpeg, image
http 测试程序,post 和get方法测试
3星 · 超过75%的资源 需积分: 9 100 浏览量
2017-11-14
16:10:36
上传
评论
收藏 87KB ZIP 举报
ccReader
- 粉丝: 16
- 资源: 18
最新资源
- 基于Java的在线宠物用品交易网站设计与实现源码
- 毕业设计-仿生六足机器人的制作全教程源码+电子元器件+程序代码+线路组件图+安装教程+搭建视频教程
- 基于ROS和webots的xrobot机械臂仿真初探C++源码
- 基于ROS的点焊机器人仿真与控制python源码+文档说明+使用说明+详细注释
- 基于vue实现的细粒度交通时空大数据分析系统+源代码+文档说明
- 安卓大作业-基于Electron的交通时空大数据分析挖掘系统客户端(Android)+源代码+文档说明+界面截图
- 基于Java的朱氏集团客户关系管理系统设计源码
- 基于C++的作业提交与批改系统设计源码
- 基于Vue2的移动端电影资讯网站设计源码
- 高分课程设计作业-基于QT的模仿宝石迷阵游戏C++源码+文档说明+界面截图
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈