求基于http协议的post、get等相关使用示例代码 求基于http协议的post、get等相关使用示例代码http协议 post get 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 一个封装类简单搞定// 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 "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(); } ------------------------------------------------------AutoCSDN签名档------------------------------------------------------ 关于WinCE的WM_PAINT消息中给BUTTON画线的问题 有没有封包编辑控件啊?什么控件?请进来看图! ClistCtrl有LVS_EX_CHECKBOXES风格。 怎么获取每个视频窗口的id? 一个很简单的问题 关于图形绘制软件的dll文件规划问题。 如何实现ClistCtrl的打印 一个问题,高手请进,急急急急急急急急急急急急急急 请各位大哥提点!如何判断其它程序的运行? 咨询一下论坛中做ATL开发的人多不多? 怎么向浏览器发送前进 后退操作消息? U盘完全拷贝
// 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 "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();
}