随笔 - 298  文章 - 377  trackbacks - 0
<2012年6月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

常用链接

留言簿(34)

随笔分类

随笔档案

文章档案

相册

收藏夹

搜索

  •  

最新评论

阅读排行榜

评论排行榜

以下是两个文件:HttpClient.h HttpClient.cpp 一个类
(错误检测部分没有加入)
使用方法
1:get方式
CHttpClient conn;
CString value="http://8crystal.com/test.asp";
value=conn.doGet(value);
2:post方式
CHttpClient conn;
CString value="http://8crystal.com/test.asp";
conn.addParam("name1","value1");
conn.addParam("name2","value2");
conn.addParam("name3","value3");
conn.addParam("name4","value4");
conn.addParam("name5","value5");
conn.addParam("name6","value6");
value=conn.doPost(value);

// HttpClient.h: interface for the CHttpClient class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_HTTPCLIENT_H__EA769DCB_AAB9_47CD_BD87_FBD6913592C5__INCLUDED_)
#define AFX_HTTPCLIENT_H__EA769DCB_AAB9_47CD_BD87_FBD6913592C5__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "wininet.h"
#include "afxinet.h"
class CHttpClient
{
public:
void addParam(CString name,CString value);
CString doPost(CString href);
CString doGet(CString href);
CHttpClient();
virtual ~CHttpClient();

private:
CString CONTENT;
int CL;
CStringList values;
CStringList names;
};

#endif // !defined(AFX_HTTPCLIENT_H__EA769DCB_AAB9_47CD_BD87_FBD6913592C5__INCLUDED_)

++++++++++++++++++++++++++++++++++++++


// HttpClient.cpp: implementation of the CHttpClient class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "emailsenderv2.h"
#include "HttpClient.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CHttpClient::CHttpClient()
{

}

CHttpClient::~CHttpClient()
{

}

CString CHttpClient::doGet(CString href)
{
CString httpsource="";
CInternetSession session1(NULL,0);
CHttpFile* pHTTPFile=NULL;
try{
pHTTPFile=(CHttpFile*)session1.OpenURL(href);
//session1.
}catch(CInternetException){
pHTTPFile=NULL;
}
if(pHTTPFile)
{
CString text;
for(int i=0;pHTTPFile->ReadString(text);i++)
{
httpsource=httpsource+text+"\r\n";
}
pHTTPFile->Close();
delete pHTTPFile;
}else
{

}
return httpsource;
}

CString CHttpClient::doPost(CString href)
{
CString httpsource="";
CInternetSession session1;
CHttpConnection* conn1=NULL;
CHttpFile* pFile = NULL;
CString strServerName;
CString strObject;
INTERNET_PORT nPort;
DWORD dwServiceType;
AfxParseURL((LPCTSTR)href,dwServiceType, strServerName, strObject, nPort);
DWORD retcode;
char* outBuff = CONTENT.GetBuffer(1000);
try
{
conn1 = session1.GetHttpConnection(strServerName,nPort);
pFile = conn1->OpenRequest(0,strObject,NULL,1,NULL,"HTTP/1.1",INTERNET_FLAG_EXISTING_CONNECT|INTERNET_FLAG_NO_AUTO_REDIRECT);
pFile -> AddRequestHeaders("Content-Type: application/x-www-form-urlencoded");
pFile -> AddRequestHeaders("Accept: */*");
pFile -> SendRequest(NULL,0,outBuff,strlen(outBuff)+1);
pFile -> QueryInfoStatusCode(retcode);
}
catch (CInternetException * e){};
if(pFile)
{
CString text;
for(int i=0;pFile->ReadString(text);i++)
{
httpsource=httpsource+text+"\r\n";
}
pFile->Close();
}else
{

}
return httpsource;
delete pFile;
delete conn1;
session1.Close();
}

void CHttpClient::addParam(CString name, CString value)
{
names.AddTail((LPCTSTR)name);
values.AddTail((LPCTSTR)value);
CString eq="=";
CString an="&";
CONTENT=CONTENT+name+eq+value+an;
CL=CONTENT.GetLength();
}

posted on 2012-06-09 21:55 聂文龙 阅读(15170) 评论(2)  编辑 收藏 引用 所属分类: Visual C++

FeedBack:
# re: VC实现HTTP协议的GET和POST方法 2013-03-02 17:15 爱问
Httpwatch studio 获取的数据
/*****************Stream************************/
POST /s HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Referer: http://cet.99sushe.com/
Accept-Language: zh-cn
Content-Type: application/x-www-form-urlencoded
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; InfoPath.2)
Host: cet.99sushe.com
Content-Length: 36
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: CNZZDATA30023677=cnzz_eid%3D1415864779-1362203966-http%253A%252F%252Fcet.99sushe.com%26ntime%3D1362210264%26cnzz_a%3D4%26retime%3D1364214548375%26sin%3D%26ltime%3D1362210545355%26rtime%3D0; searchtime=1362210583

id=372121122201234&name=%CF%F6%C0%66

/**************************************/
像这样的数据应该怎么用POST提交呀?
searchtime如何获取?
name这样的代码又是什么代码?怎么转换?
菜鸟一枚,详细解释下吧!非常感谢!
  回复  更多评论
  
# re: VC实现HTTP协议的GET和POST方法[未登录] 2013-09-27 18:56 mark
非常好。  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理