void HttpRequest(LPCTSTR lpHostName,short sPort,LPCTSTR lpUrl,LPCTSTR lpMethod,LPCTSTR lpPostData,int nPostDataLen)
{
HINTERNET hInternet,hConnect,hRequest;
hInternet = (HINSTANCE)InternetOpen(_T("User-Agent"),INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
if(!hInternet)
{
InternetCloseHandle(hInternet);
}
hConnect = (HINSTANCE)InternetConnect(hInternet,lpHostName,sPort,NULL,NULL,INTERNET_SERVICE_HTTP,0,0);
if(!hConnect)
{
if(hConnect)
InternetCloseHandle(hConnect);
if(hInternet)
InternetCloseHandle(hInternet);
}
hRequest = (HINSTANCE)HttpOpenRequest(hConnect,lpMethod,lpPostData,_T("HTTP/1.1"),lpUrl,NULL,INTERNET_FLAG_RELOAD,0);
bool ret= HttpSendRequest(hRequest,0,0,0,0);
DWORD dwErr = ::GetLastError();
if(!hRequest)
{
if(hRequest)
InternetCloseHandle(hRequest);
if(hConnect)
InternetCloseHandle(hConnect);
if(hInternet)
InternetCloseHandle(hInternet);
}
{
HINTERNET hInternet,hConnect,hRequest;
hInternet = (HINSTANCE)InternetOpen(_T("User-Agent"),INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
if(!hInternet)
{
InternetCloseHandle(hInternet);
}
hConnect = (HINSTANCE)InternetConnect(hInternet,lpHostName,sPort,NULL,NULL,INTERNET_SERVICE_HTTP,0,0);
if(!hConnect)
{
if(hConnect)
InternetCloseHandle(hConnect);
if(hInternet)
InternetCloseHandle(hInternet);
}
hRequest = (HINSTANCE)HttpOpenRequest(hConnect,lpMethod,lpPostData,_T("HTTP/1.1"),lpUrl,NULL,INTERNET_FLAG_RELOAD,0);
bool ret= HttpSendRequest(hRequest,0,0,0,0);
DWORD dwErr = ::GetLastError();
if(!hRequest)
{
if(hRequest)
InternetCloseHandle(hRequest);
if(hConnect)
InternetCloseHandle(hConnect);
if(hInternet)
InternetCloseHandle(hInternet);
}
}
CString csWebAddr("/light.jsp?cmd=login"));//带参数的
CString csUrl;
csUrl.Format(_T("http://%s:8090"),ServerIP);
HttpRequest(_T("172.168.0.2"),8090,csUrl,_T("POST"),csWebAddr,0);
注意:HttpOpenRequest说明要向www.baidu.com请求baidu_sylogo1.gif,但这个函数是不会向www.baidu.com发送任何数据的,只有调用HttpSendRequest时才会发送这个请求