随笔 - 298  文章 - 377  trackbacks - 0
<2017年5月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

常用链接

留言簿(34)

随笔分类

随笔档案

文章档案

相册

收藏夹

搜索

  •  

最新评论

阅读排行榜

评论排行榜

//上报数据
BOOL ReportData_V2(const char* szUrl, const char* szData, CString &sError)
{
CString strServer;
CString strObject;
INTERNET_PORT nPort;
DWORD dwServiceType;
if (!AfxParseURL(szUrl, dwServiceType, strServer, strObject, nPort))
{
return FALSE;
}


if (dwServiceType != AFX_INET_SERVICE_HTTPS && dwServiceType != AFX_INET_SERVICE_HTTP)
{
return FALSE;
}


HINTERNET m_hOpen;
HINTERNET m_hConnect;
HINTERNET m_hRequest;


//1. init www 
m_hOpen = InternetOpen(TEXT("PervasiveBiz"), INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
if (!m_hOpen)
{
return FALSE;
}


//2. connect
//if (!(m_hConnect = InternetConnect(m_hOpen, strServer, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, INTERNET_FLAG_RELOAD, 0)))
if (!(m_hConnect = InternetConnect(m_hOpen, strServer, nPort, NULL, NULL, dwServiceType, 0, 0)))
{
InternetCloseHandle(m_hOpen);
return FALSE;
}


//3. 创建HTTP请求句柄
LPTSTR AcceptTypes[2] = { TEXT("*/*"), NULL };
DWORD dwFlags = INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE;
if (!(m_hRequest = HttpOpenRequest(m_hConnect, _T("POST"), strObject, _T("HTTP/1.1"), NULL, (LPCTSTR*)AcceptTypes, dwFlags, 0)))
{
InternetCloseHandle(m_hConnect);
InternetCloseHandle(m_hOpen);
return FALSE;
}


//4..设置Internet参数
DWORD dwHttpTimeout = 5000;
if (!InternetSetOption(m_hRequest, INTERNET_OPTION_CONNECT_TIMEOUT, &dwHttpTimeout, sizeof(DWORD)))
{
InternetCloseHandle(m_hRequest);
InternetCloseHandle(m_hConnect);
InternetCloseHandle(m_hOpen);
return FALSE;
}
if (!InternetSetOption(m_hRequest, INTERNET_OPTION_SEND_TIMEOUT, &dwHttpTimeout, sizeof(DWORD)))
{
InternetCloseHandle(m_hRequest);
InternetCloseHandle(m_hConnect);
InternetCloseHandle(m_hOpen);
return FALSE;
}
if (!InternetSetOption(m_hRequest, INTERNET_OPTION_RECEIVE_TIMEOUT, &dwHttpTimeout, sizeof(DWORD)))
{
InternetCloseHandle(m_hRequest);
InternetCloseHandle(m_hConnect);
InternetCloseHandle(m_hOpen);;
return FALSE;
}


//5..发送请求至HTTP服务器
if (!HttpSendRequest(m_hRequest, NULL, 0, (char*)szData, strlen(szData)))
{
InternetCloseHandle(m_hRequest);
InternetCloseHandle(m_hConnect);
InternetCloseHandle(m_hOpen);
return FALSE;
}


//6..先得到响应头的长度(HTTP_QUERY_RAW_HEADERS_CRLF:接收所有由服务器返回的HEAD)
DWORD dwSize = 0;
HttpQueryInfo(m_hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &dwSize, NULL);


//再得到响应头的内容
TCHAR* lpHeaders = new TCHAR[dwSize];
if (!HttpQueryInfo(m_hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, (LPVOID)lpHeaders, &dwSize, NULL))
{
InternetCloseHandle(m_hRequest);
InternetCloseHandle(m_hConnect);
InternetCloseHandle(m_hOpen);
delete[] lpHeaders;
return FALSE;
}


//7..得到"HTTP/1.1 200 OK"中的状态码
TCHAR szHttpStatus[4] = { 0 };
memcpy(szHttpStatus, lpHeaders + strlen("HTTP/1.1 "), 3 * sizeof(TCHAR)); //200,302,...
int dwHttpStatus = atoi(szHttpStatus);
if ((dwHttpStatus < HTTP_STATUS_OK) || (dwHttpStatus > HTTP_STATUS_AMBIGUOUS))
{
InternetCloseHandle(m_hRequest);
InternetCloseHandle(m_hConnect);
InternetCloseHandle(m_hOpen);
delete[] lpHeaders;
return FALSE;
}


//读响应
TCHAR szBuff[1024+1] = { 0 };
if (!InternetReadFile(m_hRequest, szBuff, 1024, &dwSize))
{
InternetCloseHandle(m_hRequest);
InternetCloseHandle(m_hConnect);
InternetCloseHandle(m_hOpen);
delete[] lpHeaders;
return FALSE;
}
if (dwSize == 0)
{
InternetCloseHandle(m_hRequest);
InternetCloseHandle(m_hConnect);
InternetCloseHandle(m_hOpen);
delete[] lpHeaders;
return FALSE;
}




HttpQueryInfo(m_hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &dwSize, NULL);
if (!HttpQueryInfo(m_hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, (LPVOID)lpHeaders, &dwSize, NULL))
{
InternetCloseHandle(m_hRequest);
InternetCloseHandle(m_hConnect);
InternetCloseHandle(m_hOpen);
delete[] lpHeaders;
return FALSE;
}


memcpy(szHttpStatus, lpHeaders + strlen(_T("HTTP/1.1 ")), 3 * sizeof(TCHAR));
dwHttpStatus = atoi(szHttpStatus);
if ((dwHttpStatus < HTTP_STATUS_OK) || (dwHttpStatus > HTTP_STATUS_AMBIGUOUS))
{
InternetCloseHandle(m_hRequest);
InternetCloseHandle(m_hConnect);
InternetCloseHandle(m_hOpen);
delete[] lpHeaders;
return FALSE;
}


// MessageBox(NULL, lpHeaders, _T("lpHeaders"),  0);
delete[] lpHeaders;
InternetCloseHandle(m_hRequest);
InternetCloseHandle(m_hConnect);
InternetCloseHandle(m_hOpen);


return TRUE;
} @import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
posted on 2017-05-11 18:07 聂文龙 阅读(328) 评论(0)  编辑 收藏 引用

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