推荐.NET教程:
ASP.NET
C#
开发环境
Ajax教程
控件开发
统计报表
数据库
Web服务
安装部署
CommunityServer
NHibernate
DataGrid/GridView
实用代码
VS2005
示例源码
MVC/三层
SqlHelper
入门源码
开源
CMS
Ajax/Atlas
C#.net
毕业设计
源码
经典代码
商业
本站作品
持久层
随书源码
WebService
英文/汉化
Asp.net2.0
阿牛·乐园
每天进步一点点
博客园
::
首页
::
新随笔
::
联系
::
订阅
::
管理
::
71 随笔 :: 14 文章 :: 374 评论 :: 13 Trackbacks
一个HTTP.二进制POST和HTTP指定长度接收的C++实现
//
CppSocket.cpp : Defines the entry point for the console application.
//
#include
"
stdafx.h
"
#include
<
cstdlib
>
#include
<
string
>
#include
<
algorithm
>
#include
<
iostream
>
#include
<
fstream
>
#include
<
iterator
>
#include
<
Winsock2.h
>
using
namespace
std;
#define
MS_SOCKET 1
#ifdef MS_SOCKET
#define
NULLCHAR
#define
userlog printf
#endif
int
FindContentLength(
string
header);
int
RecvHttpHeader(
int
socket,
string
&
header);
int
RecvHttpBody(
int
socket,
string
&
body,
int
contentLength);
long
Post(
const
char
*
RemoteHostIP,
int
RemoteHostPort,
const
char
*
lpURL,
const
char
*
lpExtraHeaderInfo,
string
&
strRecvBuf);
int
_tmain(
int
argc,
char
*
argv[])
{
if
(argc
<
5
)
{
cout
<<
"
usage:cppsocket ip port url bodyFile
"
<<
endl
<<
"
eg: cppsockeet 127.0.0.1 80 /a.asp a.xml
"
<<
endl;
return
1
;
}
string
ip(argv[
1
]);
string
port(argv[
2
]);
string
url(argv[
3
]);
string
bodyFile(argv[
4
]);
string
headerFile;
if
(argc
>
5
)
{
headerFile
=
argv[
5
];
}
fstream fsbody(bodyFile.c_str());
if
(fsbody.good())
{
fsbody.unsetf(ios::skipws);
istream_iterator
<
char
>
iterbody(fsbody);
string
strbody(iterbody,istream_iterator
<
char
>
());
string
strRecv;
#ifdef MS_SOCKET
WSADATA wsaData;
int
iResult
=
WSAStartup(MAKEWORD(
2
,
2
),
&
wsaData);
if
(iResult
!=
NO_ERROR)
cout
<<
"
Error at WSAStartup()
"
<<
endl;
#endif
Post(ip.c_str(),(
int
)std::strtol(port.c_str(),NULL,
10
),url.c_str(),strbody.c_str(),strRecv);
#ifdef MS_SOCKET
WSACleanup();
#endif
cout
<<
strRecv;
//
cin.get();
}
else
{
cout
<<
"
can't open file
"
<<
bodyFile
<<
"
to read
"
<<
endl;
return
2
;
}
return
0
;
}
long
Post(
const
char
*
RemoteHostIP,
int
RemoteHostPort,
const
char
*
lpURL,
const
char
*
lpExtraHeaderInfo,
string
&
strRecvBuf)
{
int
SocketId,Result,optval
=
1
;
struct
sockaddr_in sServerAddr;
struct
linger mylinger;
sServerAddr.sin_family
=
AF_INET;
sServerAddr.sin_addr.s_addr
=
inet_addr( RemoteHostIP );
//
sServerAddr->sin_addr.s_addr = INADDR_ANY;
sServerAddr.sin_port
=
htons( RemoteHostPort );
#ifdef MS_SOCKET
if
((SocketId
=
socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))
<
0
)
#else
if
((SocketId
=
socket(AF_INET,SOCK_STREAM,
0
))
<
0
)
#endif
{
return
-
1
;
}
mylinger.l_onoff
=
1
;
mylinger.l_linger
=
0
;
setsockopt(SocketId, SOL_SOCKET, SO_LINGER,
(
char
*
)
&
mylinger,
sizeof
(
struct
linger));
#ifndef MS_SOCKET
sigset(SIGPIPE,SIG_IGN);
#endif
optval
=
1
;
setsockopt(SocketId, SOL_SOCKET, SO_KEEPALIVE,
(
char
*
)
&
optval,
sizeof
(
int
));
Result
=
connect(SocketId,(
struct
sockaddr
*
)
&
sServerAddr,
sizeof
(
struct
sockaddr_in));
if
( Result
!=
0
)
{
shutdown( SocketId,
2
);
#ifdef MS_SOCKET
::closesocket(SocketId);
#else
close( SocketId );
#endif
userlog(
"
function connect error, return Value is %d
"
,Result);
return
-
1
;
}
//
//
Format the HTTP request
//
//
request buffer
char
szGetBuffer[
8096
];
//
input http command into request buffer and format it;
sprintf(szGetBuffer,
"
POST %s HTTP/1.0\r\n
""
Content-Type:text/html;charset=gb2312;\r\n
""
Content-Length:%u\r\n
"
"
\r\n
"
"
%s
"
"
\r\n\0
"
,
lpURL, strlen(lpExtraHeaderInfo), lpExtraHeaderInfo);
//
发送Post请求
Result
=
send(SocketId, szGetBuffer, strlen(szGetBuffer),
0
);
userlog(
"
In Post function , the szGetBuffer is %s
"
,szGetBuffer);
if
( Result
==
-
1
)
{
shutdown( SocketId,
2
);
#ifdef MS_SOCKET
::closesocket(SocketId);
#else
close( SocketId );
#endif
userlog(
"
function send error, return Value is %d
"
,Result);
return
-
1
;
}
//
userlog("function send success, the send string is \n %s \n return Value is %d",szGetBuffer,Result);
/**/
/*
* 接受Post请求的返回结果
*/
//
recv http header
string
header;
int
headerLength
=
RecvHttpHeader(SocketId,header);
if
(headerLength
>
0
)
{
int
contentLength
=
FindContentLength(header);
if
(contentLength
>
0
)
{
string
strBody;
RecvHttpBody(SocketId,strBody,contentLength);
userlog(
"
the sRecvBuf is %s
"
,strBody.c_str());
}
}
else
{
userlog(
"
the return string is not http protptype
"
);
}
#ifdef MS_SOCKET
::closesocket(SocketId);
#else
close( SocketId );
#endif
return
0
;
//
正常返回
}
int
FindContentLength(
string
header)
{
std::transform(header.begin(),header.end(),header.begin(),(
int
(
*
)(
int
)) tolower);
string
::size_type pos
=
header.find(
"
content-length
"
,
0
);
if
(pos
!=
string
::npos)
{
string
::size_type posEnd
=
header.find(
"
\r\n
"
,pos);
string
contentString
=
header.substr(pos,posEnd
-
pos);
userlog(contentString.c_str());
pos
=
contentString.find(
"
:
"
,
0
);
string
strLength
=
contentString.substr(pos
+
1
);
return
(
int
)std::strtol(strLength.c_str(),NULL,
10
);
}
return
0
;
}
int
RecvHttpHeader(
int
socket,
string
&
header)
{
header.clear();
char
chRecvBuf[
1
];
char
endBytes[]
=
{
13
,
10
,
13
,
10
}
;
int
posCompare
=
0
;
while
(
true
)
{
int
b
=
recv(socket,chRecvBuf,
1
,
0
);
if
(b
==
-
1
)
return
-
1
;
header.append(chRecvBuf,
1
);
if
(endBytes[posCompare]
==
chRecvBuf[
0
])
{
posCompare
++
;
if
(posCompare
==
sizeof
(endBytes))
{
break
;
}
}
else
{
posCompare
=
0
;
}
}
return
header.length();
}
int
RecvHttpBody(
int
socket,
string
&
body,
int
contentLength)
{
body.clear();
char
chRecvBuf[
1024
];
while
(body.length()
<
contentLength)
{
memset(chRecvBuf,
0
,
sizeof
(chRecvBuf));
int
b
=
recv(socket,chRecvBuf,
sizeof
(chRecvBuf)
-
1
,
0
);
if
(b
==
-
1
)
return
-
1
;
body.append(chRecvBuf);
}
return
body.length();
}
posted on 2007-08-13 09:02
阿牛
阅读(473)
评论(1)
编辑
收藏
所属分类:
C++
评论
#1楼
2007-08-19 09:36
误闯牛棚的人 [未注册用户]
竟然有一个多月没来了,纯支持!
阿牛,加油啊!
回复
引用
新用户注册
刷新评论列表
标题
姓名
主页
Email
(博主才能看到)
验证码
*
看不清,换一张
[
登录
][
注册
]
内容(请不要发表任何与政治相关的内容)
博客园首页
新闻频道
社区
小组
博问
网摘
闪存
Remember Me?
登录
使用高级评论
新用户注册
返回页首
恢复上次提交
[使用Ctrl+Enter键可以直接提交]
成果网帮您增加网站收入
相关文章:
使用Visual Studio2005入门.Net2.0系列视频教程
prototype.js 1.4版开发者手册(强烈推荐)
关于IE问题,请教和求救
C#下如何实现服务器+客户端的聊天程序
ASP.NET AJAX入门系列
智能实验室-全能优化(Guardio) 3.8.0.525-全面优化和保护您的计算机!
相关链接:
所属分类的其他文章:
C与C#通讯加密之C语言DES的cbc pkcs7的实现(二)
C与C#通讯加密之C语言DES的cbc pkcs7的实现
一个HTTP.二进制POST和HTTP指定长度接收的C++实现
修复WebCompiler被某病毒后无法打开电子书的问题
用C++语言实现目录文件的非递归遍历并用仿函数来进行文件操作
命令行查询IE和Explorer的打开文件记录并保存到文件
[原]用三行代码实现对音量的控制,实现增大,减小,静音
[原]改动CImage以实现以指定的质量保存Jpeg图像
Window下可编译的 MD5Coll.c
UTF8ToBytes
最新IT新闻:
JavaScript将成Silverlight的最大对手?
没有谷歌就活不下去的四个网站
Google 10周岁生日
祝Google 10周岁生日快乐
Google十年市值达1500亿美元 创造奇迹
<
2007年8月
>
日
一
二
三
四
五
六
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
与我联系
发短消息
搜索
常用链接
我的随笔
我的空间
我的短信
我的评论
更多链接
我的参与
我的新闻
最新评论
我的标签
留言簿
(5)
给我留言
查看留言
我参与的团队
O/R Mapping团队(0/207)
随笔分类
(99)
ASP.net(22)
(rss)
BAT(1)
(rss)
C#(20)
(rss)
C++(10)
(rss)
DHTML,JS(19)
(rss)
Java(2)
(rss)
TSQL(6)
(rss)
工作流(1)
(rss)
其它语言(5)
(rss)
生活(13)
(rss)
随笔档案
(71)
2008年5月 (1)
2008年3月 (1)
2007年12月 (2)
2007年11月 (2)
2007年9月 (1)
2007年8月 (5)
2007年7月 (9)
2007年6月 (6)
2007年5月 (5)
2007年4月 (5)
2007年3月 (8)
2006年11月 (2)
2006年9月 (1)
2006年8月 (4)
2006年7月 (4)
2006年6月 (2)
2006年5月 (2)
2006年4月 (7)
2006年3月 (2)
2005年11月 (2)
文章分类
(25)
ASP(2)
(rss)
ASP.NET(2)
(rss)
C#(3)
(rss)
CSS(1)
(rss)
DHTML,JS(7)
(rss)
MSSql(4)
(rss)
VC/C++(1)
(rss)
收藏(5)
(rss)
文章档案
(14)
2007年7月 (1)
2006年11月 (1)
2006年9月 (3)
2006年5月 (1)
2006年3月 (2)
2005年11月 (6)
收藏夹
(32)
C#(10)
(rss)
Database(2)
(rss)
javascript(3)
(rss)
Log4Net(3)
(rss)
Remoting(3)
(rss)
报表
(rss)
创业(4)
(rss)
工作流(4)
(rss)
网页(3)
(rss)
友情链接