SDK网络连接/多线程示例代码
2010年06月09日
[code] static BOOL SendMsg(wstring url)
{
DWORD dwNetWorkStatus = QueryNetWorkStatus();
HINTERNET hSession = NULL,hConnection = NULL,hOpenRequest = NULL;
//如果没有网络则连接网络
if (dwNetWorkStatus == NETWORK_NONE)
{
Dial_StartGprsConnect2(0,GPRS_FORCE_APP_TYPE);
//联网后重新查询网络类型
dwNetWorkStatus = QueryNetWorkStatus();
}
//判定是否为cmwap,使用代理
if (dwNetWorkStatus == NETWORK_EDGE_PROXY)
hSession = InternetOpen(L"Meizu", INTERNET_OPEN_TYPE_PROXY, L"10.0.0.172:80", NULL, 0);
else
hSession = InternetOpen(L"Meizu", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
string url_utf8 = MzConvert::UnicodeToUTF8(url);//编码
url = ::MzConvert::string2wstring(url_utf8);
//打开链接,获取文件
HINTERNET hHttpFile = InternetOpenUrlW(hSession, url.c_str(), NULL, 0,INTERNET_FLAG_RELOAD, 0);
//读取文件内容
char buf[1024]={0};
DWORD dwRead=0;
BOOL ReadReturn=InternetReadFile(hHttpFile,(LPVOID)buf, 1024+1,&dwRead);
wstring str = MzConvert::UTF8ToUnicode(string(buf));//解码
//关闭连接
InternetCloseHandle(hSession);
return true;
}[/code]这里面编码解码很重要的,不然中文乱码问题很头疼!
多线程:
声明:static DWORD WINAPI ThreadProc (LPVOID pArg);
static wstring SEND = L"http://quickfetion.appspot.com/sendmsg?username=?;password=?;sendto=?;message=?";
定义:static DWORD WINAPI ThreadProc (LPVOID pArg)
{
wstring *c = (wstring*)pArg;
wstring url = wstring(*c);
CQuickFxMainWnd::SendMsg(url);
return 1;
}
调用:HANDLE hThread = CreateThread(NULL, 0, ::ThreadProc,&SEND, 0, 0);
这里注意函数定义形式,static关键字,还有全局概念,否则出现线程阻塞,不可访问资源的错误!
个人理解,各位高手莫要见笑!!!
附上编码解码的头文件:[code]
using namespace std;
class MzConvert
{
public:
static wstring string2wstring(const string& s)
{
int len;
int slength = (int)s.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
std::wstring r(buf);
delete[] buf;
return r;
}
static wstring ANSIToUnicode( const string& str )
{
int len = str.length();
int unicodeLen = ::MultiByteToWideChar( CP_ACP, 0, str.c_str(), -1, NULL, 0 );
wchar_t * pUnicode;
pUnicode = new wchar_t[unicodeLen+1];
memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
MultiByteToWideChar( CP_ACP, 0, str.c_str(), -1, (LPWSTR)pUnicode, unicodeLen );
wstring rt = ( wchar_t* )pUnicode;
delete pUnicode;
return rt;
}
static string UnicodeToANSI( const wstring& str )
{
char* pElementText;
int iTextLen;
// wide char to multi char
iTextLen = WideCharToMultiByte( CP_ACP, 0, str.c_str(), -1, NULL, 0, NULL, NULL );
pElementText = new char[iTextLen + 1];
memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) );
WideCharToMultiByte( CP_ACP, 0, str.c_str(), -1, pElementText, iTextLen, NULL, NULL );
string strText = pElementText;
delete[] pElementText;
return strText;
}
static wstring UTF8ToUnicode( const string& str )
{
int len = str.length();
int unicodeLen = ::MultiByteToWideChar( CP_UTF8, 0, str.c_str(), -1, NULL, 0 );
wchar_t * pUnicode;
pUnicode = new wchar_t[unicodeLen+1];
memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
MultiByteToWideChar( CP_UTF8, 0, str.c_str(), -1, (LPWSTR)pUnicode, unicodeLen );
wstring rt = ( wchar_t* )pUnicode;
delete pUnicode;
return rt;
}
static string UnicodeToUTF8( const wstring& str )
{
char* pElementText;
int iTextLen;
// wide char to multi char
iTextLen = WideCharToMultiByte( CP_UTF8, 0, str.c_str(), -1, NULL, 0, NULL, NULL );
pElementText = new char[iTextLen + 1];
memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) );
WideCharToMultiByte( CP_UTF8, 0, str.c_str(), -1, pElementText, iTextLen, NULL, NULL );
string strText = pElementText;
delete[] pElementText;
return strText;
}
};[/code]
[[i] 本帖最后由 zhaxg 于 2009-10-13 08:59 编辑 [/i]]
2010年06月09日
[code] static BOOL SendMsg(wstring url)
{
DWORD dwNetWorkStatus = QueryNetWorkStatus();
HINTERNET hSession = NULL,hConnection = NULL,hOpenRequest = NULL;
//如果没有网络则连接网络
if (dwNetWorkStatus == NETWORK_NONE)
{
Dial_StartGprsConnect2(0,GPRS_FORCE_APP_TYPE);
//联网后重新查询网络类型
dwNetWorkStatus = QueryNetWorkStatus();
}
//判定是否为cmwap,使用代理
if (dwNetWorkStatus == NETWORK_EDGE_PROXY)
hSession = InternetOpen(L"Meizu", INTERNET_OPEN_TYPE_PROXY, L"10.0.0.172:80", NULL, 0);
else
hSession = InternetOpen(L"Meizu", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
string url_utf8 = MzConvert::UnicodeToUTF8(url);//编码
url = ::MzConvert::string2wstring(url_utf8);
//打开链接,获取文件
HINTERNET hHttpFile = InternetOpenUrlW(hSession, url.c_str(), NULL, 0,INTERNET_FLAG_RELOAD, 0);
//读取文件内容
char buf[1024]={0};
DWORD dwRead=0;
BOOL ReadReturn=InternetReadFile(hHttpFile,(LPVOID)buf, 1024+1,&dwRead);
wstring str = MzConvert::UTF8ToUnicode(string(buf));//解码
//关闭连接
InternetCloseHandle(hSession);
return true;
}[/code]这里面编码解码很重要的,不然中文乱码问题很头疼!
多线程:
声明:static DWORD WINAPI ThreadProc (LPVOID pArg);
static wstring SEND = L"http://quickfetion.appspot.com/sendmsg?username=?;password=?;sendto=?;message=?";
定义:static DWORD WINAPI ThreadProc (LPVOID pArg)
{
wstring *c = (wstring*)pArg;
wstring url = wstring(*c);
CQuickFxMainWnd::SendMsg(url);
return 1;
}
调用:HANDLE hThread = CreateThread(NULL, 0, ::ThreadProc,&SEND, 0, 0);
这里注意函数定义形式,static关键字,还有全局概念,否则出现线程阻塞,不可访问资源的错误!
个人理解,各位高手莫要见笑!!!
附上编码解码的头文件:[code]
using namespace std;
class MzConvert
{
public:
static wstring string2wstring(const string& s)
{
int len;
int slength = (int)s.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
std::wstring r(buf);
delete[] buf;
return r;
}
static wstring ANSIToUnicode( const string& str )
{
int len = str.length();
int unicodeLen = ::MultiByteToWideChar( CP_ACP, 0, str.c_str(), -1, NULL, 0 );
wchar_t * pUnicode;
pUnicode = new wchar_t[unicodeLen+1];
memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
MultiByteToWideChar( CP_ACP, 0, str.c_str(), -1, (LPWSTR)pUnicode, unicodeLen );
wstring rt = ( wchar_t* )pUnicode;
delete pUnicode;
return rt;
}
static string UnicodeToANSI( const wstring& str )
{
char* pElementText;
int iTextLen;
// wide char to multi char
iTextLen = WideCharToMultiByte( CP_ACP, 0, str.c_str(), -1, NULL, 0, NULL, NULL );
pElementText = new char[iTextLen + 1];
memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) );
WideCharToMultiByte( CP_ACP, 0, str.c_str(), -1, pElementText, iTextLen, NULL, NULL );
string strText = pElementText;
delete[] pElementText;
return strText;
}
static wstring UTF8ToUnicode( const string& str )
{
int len = str.length();
int unicodeLen = ::MultiByteToWideChar( CP_UTF8, 0, str.c_str(), -1, NULL, 0 );
wchar_t * pUnicode;
pUnicode = new wchar_t[unicodeLen+1];
memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
MultiByteToWideChar( CP_UTF8, 0, str.c_str(), -1, (LPWSTR)pUnicode, unicodeLen );
wstring rt = ( wchar_t* )pUnicode;
delete pUnicode;
return rt;
}
static string UnicodeToUTF8( const wstring& str )
{
char* pElementText;
int iTextLen;
// wide char to multi char
iTextLen = WideCharToMultiByte( CP_UTF8, 0, str.c_str(), -1, NULL, 0, NULL, NULL );
pElementText = new char[iTextLen + 1];
memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) );
WideCharToMultiByte( CP_UTF8, 0, str.c_str(), -1, pElementText, iTextLen, NULL, NULL );
string strText = pElementText;
delete[] pElementText;
return strText;
}
};[/code]
[[i] 本帖最后由 zhaxg 于 2009-10-13 08:59 编辑 [/i]]