如何使用VC进行HTTP连接
2008-12-12 20:23
603 查看
CString httpRequest(char* lpHostName, short sPort, char* lpUrl, char* lpMethod, char* lpPostDara, int nPostDataLen)
{
HINTERNET hInternet, hConnect, hRequest;
BOOL bRet;
CString strResponse;
hInternet = InternetOpen(TEXT("User-Agent:"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (hInternet)
{
hConnect = InternetConnect(hInternet, lpHostName, sPort, NULL, TEXT("HTTP/1.1"), INTERNET_SERVICE_HTTP, 0, 0);
if (hConnect)
{
hRequest = HttpOpenRequest(hConnect, lpMethod, lpUrl, TEXT("HTTP/1.1"), NULL, NULL, INTERNET_FLAG_RELOAD, 0);
if (hRequest)
{
bRet = HttpSendRequest(hRequest, NULL, 0, lpPostDara, nPostDataLen);
while (TRUE)
{
char cReadBuf[4096];
unsigned long lNumOfByteRead;
bRet = InternetReadFile(hRequest, cReadBuf, sizeof(cReadBuf)-1, &lNumOfByteRead);
if(!bRet || !lNumOfByteRead)
{break;}
cReadBuf[lNumOfByteRead]=0;
strResponse = strResponse + cReadBuf;
}
}
InternetCloseHandle(hRequest);
}
InternetCloseHandle(hConnect);
}
InternetCloseHandle(hInternet);
return strResponse;
}
测试的时候,使用如下代码调用:
CString strResponse = httpRequest("www.hao123.com",80,NULL,"POST",NULL,0);
{
HINTERNET hInternet, hConnect, hRequest;
BOOL bRet;
CString strResponse;
hInternet = InternetOpen(TEXT("User-Agent:"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (hInternet)
{
hConnect = InternetConnect(hInternet, lpHostName, sPort, NULL, TEXT("HTTP/1.1"), INTERNET_SERVICE_HTTP, 0, 0);
if (hConnect)
{
hRequest = HttpOpenRequest(hConnect, lpMethod, lpUrl, TEXT("HTTP/1.1"), NULL, NULL, INTERNET_FLAG_RELOAD, 0);
if (hRequest)
{
bRet = HttpSendRequest(hRequest, NULL, 0, lpPostDara, nPostDataLen);
while (TRUE)
{
char cReadBuf[4096];
unsigned long lNumOfByteRead;
bRet = InternetReadFile(hRequest, cReadBuf, sizeof(cReadBuf)-1, &lNumOfByteRead);
if(!bRet || !lNumOfByteRead)
{break;}
cReadBuf[lNumOfByteRead]=0;
strResponse = strResponse + cReadBuf;
}
}
InternetCloseHandle(hRequest);
}
InternetCloseHandle(hConnect);
}
InternetCloseHandle(hInternet);
return strResponse;
}
测试的时候,使用如下代码调用:
CString strResponse = httpRequest("www.hao123.com",80,NULL,"POST",NULL,0);
相关文章推荐
- 如何在VC中WININET使用HTTP的POSTA和GET方法
- 使用POST、GET、AsyncHttpClient创造服务端用手机端来连接进行登录
- 如何使用HttpUnit进行上传文件测试
- 【hadoop】Hadoop学习笔记(九):如何在windows上使用eclipse远程连接hadoop进行程序开发
- Apache HttpClient 如何进行https连接
- 使用HttpWebRequest进行请求时发生错误:基础连接已关闭,发送时发生错误处理
- Android HttpUrlConnection如何使用Https连接
- 如何使用window自带的远程桌面连接进行远程文件的上传和下载
- 如何使用专用管理连接进行问题诊断和处理 【摘自SQL Server在线帮助】
- VC使用mysql.h的接口头文件进行连接mysql数据库开发的完整解决方案
- 在VC中WININET如何使用HTTP的POST方法
- VC下如何使用GDI+进行图像程序设计
- 使用VC进行程序开发时,如何引用外部的库
- VC下如何使用GDI+进行图像程序设计
- 如何在进行数据库连接到服务器时使用端口号
- 如何使用ASIHTTPRequest来进行网络请求的
- 如何使用github和git进行团队合作开发(队友和owner的仓库连接)
- 如何在windows上使用eclipse远程连接hadoop进行程序开发
- 如何使用VC进行远程调试(Remote Debug)
- 如何禁止使用本地administrator进行共享连接