#include <initguid.h>
#include <windows.h>
#include <commctrl.h>
#include <Wininet.h>
#include <Connmgr.h>
#include <tsp.h>
#pragma comment(lib,"cellcore.lib")
#pragma comment(lib,"Wininet.lib")
void DestroyConnection( HANDLE hConnect )
{
// If a connection exists, destroy it
if(NULL != hConnect )
{
ConnMgrReleaseConnection(hConnect, FALSE);
}
}
//拨号联网
int CreateConnection( HANDLE *phConnection )
{
HRESULT hResult;
// HANDLE phConnection;
DWORD dwStatus;
DWORD dwResult;
CONNMGR_DESTINATION_INFO cdi;
int i;
for(i = 0; SUCCEEDED(ConnMgrEnumDestinations(i, &cdi)); i++)
{
WriteLog(cdi.szDescription);
if (0 == wcscmp(cdi.szDescription, _T("WAP")))
{
break;
}
}
CString strLog;
hResult = ConnMgrEnumDestinations(i, &cdi);
if (FAILED(hResult))
{
strLog.Format(_T("lasterr:%d"),GetLastError());
WriteLog(_T("失败1"));
WriteLog((TCHAR*)(LPCTSTR)strLog);
return FALSE;
}
CONNMGR_CONNECTIONINFO ConnInfo;
ZeroMemory( &ConnInfo, sizeof( ConnInfo ) );
ConnInfo.cbSize = sizeof( ConnInfo );
ConnInfo.dwParams = CONNMGR_PARAM_GUIDDESTNET;
ConnInfo.dwPriority = CONNMGR_PRIORITY_USERINTERACTIVE;
ConnInfo.dwFlags = CONNMGR_FLAG_PROXY_HTTP | CONNMGR_FLAG_PROXY_WAP | CONNMGR_FLAG_PROXY_SOCKS4 | CONNMGR_FLAG_PROXY_SOCKS5;
ConnInfo.bExclusive = FALSE;
ConnInfo.bDisabled = FALSE;
ConnInfo.guidDestNet = IID_DestNetWAP;
//STLOG_WRITE(_T("ConnectToHttpServer/n") );
//CONNMGR_CONNECTIONINFO connection;
//ZeroMemory(&connection, sizeof(connection));
//connection.cbSize = sizeof(connection);
//connection.dwParams = CONNMGR_PARAM_GUIDDESTNET;
//connection.dwFlags = CONNMGR_FLAG_PROXY_HTTP | CONNMGR_FLAG_PROXY_WAP | CONNMGR_FLAG_PROXY_SOCKS4 | CONNMGR_FLAG_PROXY_SOCKS5;
//connection.dwPriority = CONNMGR_PRIORITY_USERBACKGROUND;
//connection.bExclusive = FALSE;
//connection.bDisabled = FALSE;
connection.guidDestNet = IID_DestNetInternet;
//connection.guidDestNet = IID_DestNetInternet;
hResult = ConnMgrEstablishConnectionSync( &ConnInfo, phConnection, 60000, &dwStatus );
//hResult = ConnMgrEstablishConnection(&ConnInfo, phConnection);
ConnMgrConnectionStatus(*phConnection,&dwStatus);
strLog.Format(_T("status:%d"),dwStatus);
WriteLog(_T("是否成功连接"));
WriteLog((TCHAR*)(LPCTSTR)strLog);
if (dwStatus == CONNMGR_STATUS_CONNECTED)
{
}
if (FAILED(hResult) )
{
//STLOG_WRITE(_T("ConnMgrEstablishConnection is error/n") );
// dwResult = GetLastError();
strLog.Format(_T("lasterr:%d status:%d"),GetLastError(),dwStatus);
WriteLog(_T("失败2"));
WriteLog((TCHAR*)(LPCTSTR)strLog);
return S_FALSE;
}
else
{
}
return S_OK;
}
//winnet通过wap通讯
BOOL ConnectToHttpGetServer( LPCTSTR lpszServerName, LPCTSTR lpszObjectName )
{
BOOL bResult;
DWORD dwResult;
HANDLE hConnection;
DWORD dwWritten;
LPTSTR szReadBuffer;
DWORD dwNumberOfBytesRead;
int nContentLength;
DWORD dwTotalRead;
hConnection = NULL;
bResult = FALSE;
nContentLength = 0;
dwNumberOfBytesRead = 0;
dwTotalRead = 0;
dwWritten = 0;
CString strLog;
if( CreateConnection( &hConnection ) != S_OK ) //先联网
{
return false;
}
//HINTERNET hInternet = InternetOpen(L"PhoneReader", PRE_CONFIG_INTERNET_ACCESS, NULL, INTERNET_INVALID_PORT_NUMBER, 0);
//通过电信wap代理服务器
HINTERNET hInternet = InternetOpen( _T("PhoneReader"), INTERNET_OPEN_TYPE_PROXY, _T("10.0.0.200:80"), NULL, 0);
if ( hInternet == NULL )
{
strLog.Format(_T("lasterr:%d"),GetLastError());
WriteLog(_T("失败4"));
WriteLog((TCHAR*)(LPCTSTR)strLog);
return bResult;
}
DWORD time_out = 3;
InternetSetOption(hInternet, INTERNET_OPTION_CONNECT_TIMEOUT, &time_out, sizeof(time_out));
InternetSetOption(hInternet, INTERNET_OPTION_CONTROL_SEND_TIMEOUT, &time_out, sizeof(time_out));
InternetSetOption(hInternet, INTERNET_OPTION_CONTROL_RECEIVE_TIMEOUT, &time_out, sizeof(time_out));
InternetSetOption(hInternet, INTERNET_OPTION_DATA_SEND_TIMEOUT, &time_out, sizeof(time_out));
InternetSetOption(hInternet, INTERNET_OPTION_DATA_RECEIVE_TIMEOUT, &time_out, sizeof(time_out));
InternetSetOption(hInternet, INTERNET_OPTION_RECEIVE_TIMEOUT, &time_out, sizeof(time_out));
//连接自己的服务器
HINTERNET hInternetConnect = InternetConnect( hInternet, _T("183.37.46.18"), 9988, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1 );
if ( hInternetConnect == NULL )
{
strLog.Format(_T("lasterr:%d"),GetLastError());
WriteLog(_T("失败5"));
WriteLog((TCHAR*)(LPCTSTR)strLog);
return bResult;
}
//TCHAR szAccept[] = L"*/*"; //we accept everything...
//LPTSTR AcceptTypes[2]={0};
//AcceptTypes[0]=szAccept;
HINTERNET hRequest = HttpOpenRequest(hInternetConnect,_T("POST"),NULL, L"HTTP/1.0",NULL,NULL, INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_PRAGMA_NOCACHE, 1);
if (!hRequest)
{
strLog.Format(_T("lasterr:%d"),GetLastError());
WriteLog(_T("失败6"));
WriteLog((TCHAR*)(LPCTSTR)strLog);
return bResult;
}
TCHAR cData[] = _T("12457");
BOOL bSuccess = HttpSendRequest(hRequest, NULL, 0, cData, sizeof(cData) );
if ( !bSuccess )
{
strLog.Format(_T("lasterr:%d"),GetLastError());
WriteLog(_T("失败7"));
WriteLog((TCHAR*)(LPCTSTR)strLog);
return bResult;
}
TCHAR szHeader[ 1000 ];
ZeroMemory( szHeader, sizeof( szHeader ) );
DWORD dwHeaderLength = sizeof( szHeader );
// HTTP_QUERY_RAW_HEADERS
BOOL bQuery=HttpQueryInfo(hRequest, HTTP_QUERY_CONTENT_LENGTH, szHeader, &dwHeaderLength, NULL);
if ( !bQuery )
{
strLog.Format(_T("lasterr:%d"),GetLastError());
WriteLog(_T("失败8"));
WriteLog((TCHAR*)(LPCTSTR)strLog);
return bResult;
}
nContentLength = _wtoi( szHeader );
HANDLE hFile = CreateFile( GOOGLEWEBPAGE, GENERIC_READ|GENERIC_WRITE, 0, NULL,CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
if ( hFile == INVALID_HANDLE_VALUE )
{
strLog.Format(_T("lasterr:%d"),GetLastError());
WriteLog(_T("失败9"));
WriteLog((TCHAR*)(LPCTSTR)strLog);
return bResult;
}
szReadBuffer = (LPTSTR) malloc( 1024 );
ZeroMemory(szReadBuffer, 1024 );
while( TRUE )
{
bResult = InternetReadFile(hRequest, szReadBuffer, 1024, &dwNumberOfBytesRead);
if ( bResult )
{
if ( dwTotalRead == nContentLength && dwNumberOfBytesRead == 0 )
{
bResult = TRUE;
break;
}
else
{
SetFilePointer ( hFile, 0, NULL, FILE_END );
WriteFile( hFile,(LPBYTE)szReadBuffer, dwNumberOfBytesRead, &dwWritten, NULL );
dwTotalRead = dwTotalRead + dwNumberOfBytesRead;
}
}
else
{
strLog.Format(_T("lasterr:%d"),GetLastError());
WriteLog(_T("失败10"));
WriteLog((TCHAR*)(LPCTSTR)strLog);
bResult = FALSE;
break;
}
}
CloseHandle(hFile);
free(szReadBuffer);
InternetCloseHandle( hInternetConnect );
InternetCloseHandle( hInternet );
DestroyConnection( hConnection );
//STLOG_WRITE(_T("www.devdiv.net/n") );
AfxMessageBox(_T("成功"));
return bResult;
}
//socket通过wap通讯
#include <Winsock.h>
HANDLE hConnection = NULL;
if( CreateConnection( &hConnection ) != S_OK ) //联网
{
return ;
}
CString strLog;
WSADATA info;
if (WSAStartup(MAKEWORD(2,0), &info) != 0)
{
strLog.Format(_T("err1 laseterr:%d/r/n"),GetLastError());
WriteLog((TCHAR*)(LPCTSTR)strLog);
WSACleanup();
return;
}
SOCKET sockHandle = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in sa;
memset(&sa, 0, sizeof(sockaddr_in));
sa.sin_family = AF_INET;
sa.sin_port = htons(9988);
sa.sin_addr.s_addr = inet_addr("183.37.46.18");
int nresult = connect(sockHandle,(sockaddr*)&sa,sizeof(sa)); //直接连接自己的服务器
if (SOCKET_ERROR == nresult)
{
strLog.Format(_T("err2 laseterr:%d/r/n"),GetLastError());
WriteLog((TCHAR*)(LPCTSTR)strLog);
WSACleanup();
return;
}
Sleep(100);
char cSend[] = "123456";
int nlen = send(sockHandle,(char*)cSend,strlen(cSend),0);
strLog.Format(_T("laseterr:%d [%d]/r/n"),GetLastError(),nlen);
WriteLog((TCHAR*)(LPCTSTR)strLog);
Sleep(100);
char buff[1024] = {0x00};
recv(sockHandle,buff,sizeof(buff),0);
//WSACleanup();
WriteLog((TCHAR*)buff);
WriteLog(_T("end/r/n/r/n"));