第一:
如果是MFC的应用可以使用MFC的封装类库:
#include <afxinet.h>
#pragma comment(lib, "wininet.lib")
BOOL PostXmlToUrl(string &address)
{
CInternetSession m_winet(NULL,1,INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
CHttpConnection *pConnection = NULL;
CHttpFile *pFile = NULL;
pConnection = m_winet.GetHttpConnection(address.c_str());
CString strResponse = "";
CString strDataSend;
strDataSend .Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
strDataSend.Append("<QuickStart>\n");
strDataSend.Append(" <Device>\n");
strDataSend.Append(" <Request>\n");
strDataSend.Append(" <Diag>\n");
strDataSend.Append(" <Adsu>Diag</Adsu>\n");
strDataSend.Append(" </Diag>\n");
strDataSend.Append(" </Request>\n");
strDataSend.Append(" </Device>\n");
strDataSend.Append("</QuickStart>\n");
try
{
pFile=pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST,
"/vodafoneapi/process.cgi");
pFile->AddRequestHeaders("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
pFile->AddRequestHeaders("User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.186 Safari/535.1");
pFile->AddRequestHeaders("Content-Type: application/x-www-form-urlencoded");
if (pFile->SendRequest(NULL, 0, (LPVOID)(LPCTSTR)strDataSend, strDataSend.GetLength()) == FALSE)
{
pFile->Close();
delete pFile;
pConnection->Close();
delete pConnection;
m_winet.Close();
return FALSE;
}
DWORD dwStatusCode = 0;
pFile->QueryInfoStatusCode(dwStatusCode);
if (dwStatusCode != 200)
{
pFile->Close();
delete pFile;
pConnection->Close();
delete pConnection;
m_winet.Close();
return FALSE;
}
/*char szRead[1024] = {0};
while((pFile->Read(szRead,1023)) > 0)
{
strResponse += szRead;
}*/
}
catch (CMemoryException* e)
{
pFile->Close();
delete pFile;
pConnection->Close();
delete pConnection;
m_winet.Close();
e->Delete();
return FALSE;
}
catch (CFileException* e)
{
pFile->Close();
delete pFile;
pConnection->Close();
delete pConnection;
m_winet.Close();
e->Delete();
return FALSE;
}
catch (CException* e)
{
pFile->Close();
delete pFile;
pConnection->Close();
delete pConnection;
m_winet.Close();
e->Delete();
return FALSE;
}
pFile->Close();
delete pFile;
pConnection->Close();
delete pConnection;
m_winet.Close();
return TRUE;
}
第二:如果是标准的Win32程序
#include <WinINet.h>
#pragma comment(lib, "Wininet.lib")
BOOL PostDisConnectRequest()
{
string strIpAddr("");
string strGetWay("");
char pInfo[MAX_PATH] = {0};
DWORD dwRet = GetCurtAdapterIndex(pInfo, strIpAddr, strGetWay);
if(dwRet==-1 || strGetWay.empty() || strGetWay.compare(_T("0.0.0.0"))==0)
{
return FALSE;
}
string strTail = "/vodafoneapi/process.cgi";
string strDataSend;
strDataSend += "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
strDataSend += "<QuickStart>\n";
strDataSend += " <Device>\n";
strDataSend += " <Request>\n";
strDataSend += " <Diag>\n";
strDataSend += " <Adsu>Diag</Adsu>\n";
strDataSend += " </Diag>\n";
strDataSend += " </Request>\n";
strDataSend += " </Device>\n";
strDataSend += "</QuickStart>\n";
HINTERNET hSession = InternetOpen(
"", // 1 LPCTSTR lpszCallerName
INTERNET_OPEN_TYPE_PRECONFIG, // 2 DWORD dwAccessType
"", // 3 LPCTSTR lpszProxyName
0, // 4 INTERNET_PORT nProxyPort
0 // 5 DWORD dwFlags
) ;
if(!hSession)
{
JRD_LOG("Failed to open session. \r\n");
return FALSE;
}
HINTERNET hConnect = InternetConnect(hSession, strGetWay.c_str(), INTERNET_DEFAULT_HTTP_PORT,
NULL, NULL, INTERNET_SERVICE_HTTP,NULL, NULL);
if (!hConnect)
{
JRD_LOG( "Failed to connect. \r\n");
InternetCloseHandle(hSession);
return FALSE;
}
HINTERNET hRequest = HttpOpenRequest(hConnect, "POST", strTail.c_str(),
HTTP_VERSION, NULL, NULL, INTERNET_FLAG_DONT_CACHE, 0);
if (!hRequest)
{
JRD_LOG( "Failed to open request handle. \r\n");
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
return FALSE;
}
string sHeader = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
HttpAddRequestHeaders(hRequest, sHeader.c_str(), -1, HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD);
sHeader = "User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.186 Safari/535.1";
HttpAddRequestHeaders(hRequest, sHeader.c_str(), -1, HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD);
sHeader = "Content-Type: application/x-www-form-urlencoded";
HttpAddRequestHeaders(hRequest, sHeader.c_str(), -1, HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD);
if(!HttpSendRequest(hRequest, NULL, 0, (LPVOID)strDataSend.c_str(), strlen(strDataSend.c_str())))
{
JRD_LOG("Failed to Send Request. \r\n");
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
return FALSE;
}
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
JRD_LOG( "Finished. \r\n");
return TRUE;
}
DWORD GetCurtAdapterIndex(const char* pAdapterName, string& strIpAddr, string& strGateway)
{
if (pAdapterName == NULL)
{
return -1;
}
DWORD dwIndex = -1;
ULONG ulOutBufLen = 0;
DWORD dwRet = GetAdaptersInfo(NULL, &ulOutBufLen);
if (dwRet == ERROR_BUFFER_OVERFLOW)
{
PIP_ADAPTER_INFO pAdapterInfo = reinterpret_cast<PIP_ADAPTER_INFO>(new BYTE[ulOutBufLen]);
dwRet = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen);
if (dwRet == ERROR_SUCCESS)
{
PIP_ADAPTER_INFO pAdapter = pAdapterInfo;
while (pAdapter != NULL)
{
if (_strnicmp(pAdapter->Description, pAdapterName, strlen(pAdapterName)) == 0)
{
dwIndex = pAdapter->Index;
strIpAddr = pAdapter->IpAddressList.IpAddress.String;
strGateway = pAdapter->GatewayList.IpAddress.String;//add by hao.liu 2013-8-14
JRD_LOG("Interface description==> %s, IP addr ==> %s, IP Getway ==> %s\r\n", pAdapter->Description, pAdapter->IpAddressList.IpAddress.String, strGateway);
break;
}
pAdapter = pAdapter->Next;
}
}
delete pAdapterInfo;
}
return dwIndex;
}
//该函数用来获取网卡的参数