简单的一个webclient,用来交互数据用.
#ifndef MWEBCLIENT_H_
#define MWEBCLIENT_H_
class MWebClient
{
public
:
MWebClient
();
//
成功返回
0,
数据保存在
responseBuf
中
int
doGet
(constchar* getUrl,MStringBuffer & responseBuf);
int
doPost
(constchar* postUrl,constchar* data,MStringBuffer & responseBuf);
void
setTimeOut
(int seconds);
protected
:
int
doRequest
(constchar* ip,MStringBuffer & request,MStringBuffer & responseBuf);
private
:
int
m_port;
int
m_outtime_second;
}
;
#endif /*MWEBCLIENT_H_*/
#include "MAll.h"
MWebClient:
:MWebClient()
{
m_port
=
80
;
m_outtime_second
=
10
;
}
void
MWebClient:
:setTimeOut(int seconds)
{
m_outtime_second
=
seconds;
}
int
MWebClient:
:doRequest(constchar* ip,MStringBuffer & request,MStringBuffer & responseBuf)
{
int
search_socket
=
-
1
;
struct
sockaddr_in search_server;
int
retval
=
0
, tmp_val
=
0
;
struct
timeval tv;
search_socket
=
socket
(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if
(-1== search_socket )
{
return
-
(__LINE__);
}
//
设置地址可重用
tmp_val
=
1
;
retval
=
setsockopt
(search_socket, SOL_SOCKET, SO_REUSEADDR, &tmp_val, sizeof(tmp_val));
if
( retval !=0 )
{
close
( search_socket );
search_socket
=
-
1
;
return
-
(__LINE__);
}
//
设置超时参数
int
tSec
=
m_outtime_second;
int
tUsec
=
0
;
if
(tUsec<0)
{
tUsec
=
0
;
}
tv.tv_sec
=
tSec;
tv.tv_usec
=
tUsec;
retval
=
setsockopt
( search_socket, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
if
( retval !=0 )
{
close
( search_socket );
search_socket
=
-
1
;
return
-
(__LINE__);
}
retval
=
setsockopt
( search_socket, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
if
( retval !=0 )
{
close
( search_socket );
search_socket
=
-
1
;
return
-
(__LINE__);
}
memset
((char*) &search_server, 0, sizeof(search_server));
search_server.sin_family
=
AF_INET;
search_server.sin_port
=
htons
(m_port);
search_server.sin_addr.s_addr
=
inet_addr
(ip);
retval
=
connect
( search_socket, (struct sockaddr *) &search_server, sizeof(search_server) );
if
( retval !=0 )
{
close
( search_socket );
search_socket
=
-
1
;
return
-
(__LINE__);
}
retval
=
write
( search_socket, request.toCString(),request.length());
if
(-1== retval )
{
close
( search_socket );
search_socket
=
-
1
;
return
-
(__LINE__);
}
char
strBuf[
1024
];
// int flag = 0;
// int len = 0;
while
(true){
retval
=
read
(search_socket,strBuf,sizeof(strBuf));
//printf("recv[%d]/n",retval);
if
(retval>0)
{
responseBuf.append
(strBuf,retval);
}
else
{
break
;
}
if
(retval<(int)sizeof(strBuf))
{
break
;
}
}
close
( search_socket );
search_socket
=
-
1
;
return
0
;
}
int
MWebClient:
:doPost(constchar* getUrl,constchar* data,MStringBuffer & responseBuf)
{
char
hostname[
64
];
char
getstr[
512
];
char
ip[
64
];
char
tmpGetUrl[
512
];
strncpy
(tmpGetUrl,getUrl,sizeof(tmpGetUrl)-1);
char
*
tmpP;
tmpP
=
strstr
(tmpGetUrl,"http://");
if
(tmpP!=NULL)
{
tmpP
=
tmpP
+
7
;
}
else
{
tmpP
=
tmpGetUrl;
}
char
*
tmpP2;
tmpP2
=
strstr
(tmpP,"/");
if
(tmpP2!=NULL)
{
if
(tmpP2-tmpP>=(int)sizeof(hostname))
{
return
-
(__LINE__);
}
memcpy
(hostname,tmpP,tmpP2-tmpP);
hostname[tmpP2
-
tmpP]
=
'/0'
;
strncpy
(getstr,tmpP2,sizeof(getstr)-1);
}
else
{
strncpy
(hostname,tmpP,sizeof(hostname)-1);
getstr[
0
]
=
'/'
;
getstr[
1
]
=
'/0'
;
}
struct
hostent
*
hostPtr;
hostPtr
=
gethostbyname
(hostname);
if
(hostPtr==NULL)
{
return
-
(__LINE__);
}
struct
in_addr
*
addrPtr;
addrPtr
=
(struct in_addr *) *(hostPtr->h_addr_list);
strncpy
(ip,inet_ntoa(*addrPtr),sizeof(ip)-1);
//printf("ip:[%s]/n",ip);
MStringBuffer request;
request.append
("POST ").append(getstr).append(" HTTP/1.1").append("/r/n");
request.append
("Accept: */*").append("/r/n");
request.append
("Content-Type: application/x-www-form-urlencoded").append("/r/n");
request.append
("Referer: ").append(getUrl).append("/r/n");
request.append
("User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; InfoPath.1)").append("/r/n");
request.append
("Host: ").append(hostname).append("/r/n");
request.append
("Content-Length: ").append(strlen(data)).append("/r/n");
request.append
("Connection: Keep-Alive").append("/r/n");
request.append
("/r/n");
request.append
(data);
request.append
("/r/n");
request.append
("/r/n");
//printf("request=[%s]/n",request.toCString());
return
doRequest
(ip,request,responseBuf);
}
int
MWebClient:
:doGet(constchar* getUrl,MStringBuffer & responseBuf)
{
char
hostname[
64
];
char
getstr[
512
];
char
ip[
64
];
char
tmpGetUrl[
512
];
strncpy
(tmpGetUrl,getUrl,sizeof(tmpGetUrl)-1);
char
*
tmpP;
tmpP
=
strstr
(tmpGetUrl,"http://");
if
(tmpP!=NULL)
{
tmpP
=
tmpP
+
7
;
}
else
{
tmpP
=
tmpGetUrl;
}
char
*
tmpP2;
tmpP2
=
strstr
(tmpP,"/");
if
(tmpP2!=NULL)
{
if
(tmpP2-tmpP>=(int)sizeof(hostname))
{
return
-
(__LINE__);
}
memcpy
(hostname,tmpP,tmpP2-tmpP);
hostname[tmpP2
-
tmpP]
=
'/0'
;
strncpy
(getstr,tmpP2,sizeof(getstr)-1);
}
else
{
strncpy
(hostname,tmpP,sizeof(hostname)-1);
getstr[
0
]
=
'/'
;
getstr[
1
]
=
'/0'
;
}
struct
hostent
*
hostPtr;
hostPtr
=
gethostbyname
(hostname);
if
(hostPtr==NULL)
{
return
-
(__LINE__);
}
struct
in_addr
*
addrPtr;
addrPtr
=
(struct in_addr *) *(hostPtr->h_addr_list);
strncpy
(ip,inet_ntoa(*addrPtr),sizeof(ip)-1);
//printf("ip:[%s]/n",ip);
MStringBuffer request;
request.append
("GET ").append(getstr).append(" HTTP/1.1").append("/r/n");
request.append
("Accept: */*").append("/r/n");
request.append
("Referer: ").append(getUrl).append("/r/n");
request.append
("User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; InfoPath.1)").append("/r/n");
request.append
("Host: ").append(hostname).append("/r/n");
request.append
("Connection: Keep-Alive").append("/r/n");
request.append
("/r/n");
//printf("request=[%s]/n",request.toCString());
return
doRequest
(ip,request,responseBuf);
}
--------------------------
MStringBuffer
------------------------------
一个类似string的字符缓存类,可以简单的实现一个.或用string替换.
return-(__LINE__);(又偷懒了一次,因为这边的错误码,不是我所关心的.)