gSOAP是很好的東西,它彌補了C++庫對Webservice支持的不足,讓C++的開發者能夠輕松使用Webservice,不過說輕松其實也不輕松,到目前為止,我沒有用過什么開源的庫是一到手就能很順利地使用的,總是經過了這個那個的折騰,最后才能用,雖然很多問題也都是只差那么一丁點兒,但就是那么一丁點兒卻讓人焦頭爛額。
Windows Mobile沒落了……我不止一次提起這話,我甚至懷疑我現在開發的Windows Mobile程序是不是最后一個獲得較多用戶的Windows Mobile程序,也許弄完了這個之后,也沒什么人再會涉足這個領域了。
ASMX接口定義文件
OK,廢話不說了,言歸正傳,Webservice最最最典型的應用是什么?——更新天氣,你看看Webservice的入門文章,都是拿天氣更新作為范例,而我做的這個正好也是一個天氣更新,接口是我定義的,具體就不貼出來了,總而言之我們要從WSDL這個接口文件出發,假設你已經有了這個WSDL文件了,文件名為“SSPWeatherService.asmx”。(不懂WSDL的話建議先了解下Webservice)
獲取一份gSOAP并安裝
接著要去獲取一份gSOAP的代碼,地址是gsoap2.sourceforge.net,我下載的版本是2.8.3,這是2011年6月更新的,在這前我下載了2.8.2,這兩個版本用起來還有些微小的差別,哪個更好?當然是新的更好了。
下載完之后當然是解壓縮,我是把它解壓縮到“D:\gsoap”這個路徑下。然后給系統環境變量“path”增加這么一個路徑:“D:\gsoap\gsoap\bin\win32\”,這完全是為了一會兒方便調用到“wsdl2h.exe”和“soapcpp2.exe”,否則你還得輸入exe的完整路徑。
根據asmx生成相關文件
將剛才那個WSDL文件“SSPWeatherService.asmx”放到你的工作目錄下,比如“D:\work\SSPWeatherUpdate_WS”,然后使用命令行工具,如下執行:

其中涉及到兩個命令:
>wsdl2h SSPWeatherService.asmx -o SSPWeatherService.h
>soapcpp2 SSPWeatherService.h -ID:\gsoap\gsoap\import -C -x -i
第一個命令是根據WSDL文件生成相應的頭文件,用-o參數指定生成的頭文件的名稱。
第二個命令死根據剛生成的頭文件來生成別的頭文件和cpp文件。-I后面是gSOAP的import目錄的路徑,這個是必須的,-C表示只生成客戶端代碼,這正是我們需要的,-x可以少生成一些垃圾,-i表示生成C++封裝代碼,用C++封裝好的代碼比純C代碼好用多了。
對生成內容的簡單說明
接下來你查看目錄中的文件可能是這樣:
soapC.cpp
soapH.h
soapSSPWeatherServiceSoapProxy.cpp
soapSSPWeatherServiceSoapProxy.h
soapStub.h
SSPWeatherService.asmx
SSPWeatherService.h
SSPWeatherServiceSoap.nsmap
也就是說,除了asmx和第一步生成的h文件之外,之后生成的文件有這些:
soapC.cpp
soapH.h
soapSSPWeatherServiceSoapProxy.cpp
soapSSPWeatherServiceSoapProxy.h
soapStub.h
SSPWeatherServiceSoap.nsmap
可能你還會碰到下面這幾個文件,這跟你原本的asmx的接口定義有關系:
soapSSPWeatherServiceSoap12Proxy.cpp
soapSSPWeatherServiceSoap12Proxy.h
SSPWeatherServiceSoap12.nsmap
“12”表示soap的1.2版本,你比較一下,發現這幾個文件跟上面提到的幾個文件的內容是幾乎一致的,除了里面的名稱大多都加上了“12”,對我來說這幾個文件是不需要的,所以刪除掉了。
創建一個工程
接下去當然是用Visual Studio創建一個工程來使用剛才生成的這些文件了。我創建的Project名稱為“SSPWeatherUpdate_WS”,目錄也是剛才的那個目錄,為了簡單起見,創建一個console類型的程序用來測試就行了。
然后把剛才生成的這些文件添加到這個Project中去:
soapC.cpp
soapH.h
soapSSPWeatherServiceSoapProxy.cpp
soapSSPWeatherServiceSoapProxy.h
soapStub.h
SSPWeatherService.h
也許你注意到了,asmx和nsmap文件是不需要添加的。
然后是很關鍵的一部,把D:\gsoap\gsoap目錄下的stdsoap2.h和stdsoap2.cpp復制到剛創建的工程目錄并添加到工程中去。完了之后Project里應該有這些東西:

編譯以及可能的問題
編譯一下看看,能不能通過。可能出現了一大堆的錯誤,可能你會看到這樣的出錯提示:
“1>.\soapC.cpp(16) : warning C4627: '#include "soapH.h"': skipped when looking for precompiled header use”
這是因為工程設置了使用“預編譯頭”,我們不要使用預編譯頭,工程屬性設置如下圖:

設置后rebuild,看看還有沒有什么問題?在我這里出現了這樣的錯誤提示:
1>.\soapC.cpp(850) : error C3861: 'soap_outdateTime': identifier not found
1>.\soapC.cpp(855) : error C3861: 'soap_indateTime': identifier not found
表面上看是漏掉某個頭文件,或者某個編譯選項不正確引起,但其實,這正是讓我郁悶了好久,努力了好久,最后才發現無解的問題,其中波折就不想在這里贅述了,如果你認為自己技術水平不錯,可以直接擺平這個問題的話不妨嘗試一下看,但如果時間不是很多的話我勸你就算了,直接采納我這個結論:gSOAP在WM環境下不支持WSDL中的datetime類型!我不知道這算不算bug,可能準確說“支持不佳”,如果前面的asmx是你定義的話,你就改一改,把其中的datetime類型改為string,然后自己在程序中再作轉換,如果asmx不是你定義的話,那就很不幸了,我也沒轍了,修改gSOAP的代碼是很痛苦的工作,我費了很大力氣最后都沒解決,如果你有能力解決,不妨跟我分享一下。
使用Webservice
我直接貼上我的完整代碼,希望能夠拋磚引玉。
#include "stdafx.h"
#include <string>
#include "soapSSPWeatherServiceSoapProxy.h"
#include "SSPWeatherServiceSoap.nsmap"
using namespace std;
BOOL UTF8ToTChar(const char* pUTF8Str, TCHAR* &pTChar)
{
//First, convert it to UNICODE
INT len = MultiByteToWideChar(CP_UTF8, 0, pUTF8Str, -1, NULL, 0);
WCHAR *pWC = new WCHAR[len];
MultiByteToWideChar(CP_UTF8, 0, pUTF8Str, -1, pWC, len);
pWC[len-1] = '\0';
//Second, convert UNICODE to TCHAR
#ifdef UNICODE
pTChar = pWC;
#else
len = WideCharToMultiByte(CP_ACP, 0, pWC, -1, NULL, 0, NULL, NULL);
pTChar = new TCHAR[len];
WideCharToMultiByte(CP_ACP, 0, pWC, -1, pTChar, len, NULL, NULL);
pTChar[len-1] = '\0';
delete[] pWC;
#endif
return TRUE;
}
BOOL TCharToUTF8(const TCHAR* pTChar, char* &pUTF8Str)
{
INT len;
#ifdef UNICODE
const WCHAR* pWC = pTChar;
#else
len = MultiByteToWideChar(CP_ACP, 0, pTChar, -1, NULL, 0);
WCHAR *pWC = new WCHAR[len];
MultiByteToWideChar(CP_ACP, 0, pTChar, -1, pWC, len);
pWC[len-1] = '\0';
#endif
len = WideCharToMultiByte(CP_UTF8, 0, pTChar, -1, NULL, 0, NULL, NULL);
pUTF8Str = new char[len];
WideCharToMultiByte(CP_UTF8, 0, pTChar, -1, pUTF8Str, len, NULL, NULL);
pUTF8Str[len-1] = '\0';
#ifdef UNICODE
//
#else
delete[] pWC;
#endif
return TRUE;
}
void ReleaseChar(char* &pChar)
{
if(pChar!=NULL)
{
delete[] pChar;
pChar = NULL;
}
}
void ReleaseTChar(TCHAR* &pTChar)
{
if(pTChar!=NULL)
{
delete[] pTChar;
pTChar = NULL;
}
}
#define OUTPUT_BUFF_LEN 512
void DbgStrOut(const TCHAR *fmt,
)
{
TCHAR szOutStr[OUTPUT_BUFF_LEN];
va_list ap;
va_start(ap, fmt);
StringCbVPrintf(szOutStr, OUTPUT_BUFF_LEN, fmt, ap);
va_end(ap);
OutputDebugString(szOutStr);
}
int _tmain(int argc, _TCHAR* argv[])
{
SSPWeatherServiceSoapProxy gs(SOAP_C_UTFSTRING);
gs.soap_endpoint = "http://www.sosopi.com/weathercastservice/SSPWeatherService.asmx";
_ns1__FindCityByString input;
_ns1__FindCityByStringResponse output;
CHAR* pszUTF8;
TCHAR* pszCityCode;
TCHAR* pszCityName;
TCharToUTF8(L"閔行", pszUTF8);
input.CityToFind = pszUTF8;
ReleaseChar(pszUTF8);
if(SOAP_OK==gs.FindCityByString(&input, &output))
{
std::vector<ns1__CityInfo * >::iterator it = output.CityInfo.begin();
while (it!=output.CityInfo.end())
{
ns1__CityInfo *pCityInfo = (*it);
UTF8ToTChar(pCityInfo->CityCode.c_str(), pszCityCode);
UTF8ToTChar(pCityInfo->CityName.c_str(), pszCityName);
DbgStrOut(L"%s %s\n", pszCityCode, pszCityName);
ReleaseTChar(pszCityCode);
ReleaseTChar(pszCityName);
++it;
}
if(output.MatchCityNumber>=1)
{
_ns1__GetWeatherByCityCode input2;
_ns1__GetWeatherByCityCodeResponse output2;
input2.CityCode = output.CityInfo[0]->CityCode;
if(SOAP_OK==gs.GetWeatherByCityCode(&input2, &output2))
{
TCHAR* pszUpdateTime;
float fRtTemperature;
TCHAR* pszWindDirection;
TCHAR* pszWindForce;
UTF8ToTChar(output2.WeatherCast->UpdateTime.c_str(), pszUpdateTime);
fRtTemperature = output2.WeatherCast->RtTemperature;
UTF8ToTChar(output2.WeatherCast->RtWindDirection.c_str(), pszWindDirection);
UTF8ToTChar(output2.WeatherCast->RtWindForce.c_str(), pszWindForce);
DbgStrOut(L"Update Time : %s\n", pszUpdateTime);
DbgStrOut(L"Temperature : %.1f\n", fRtTemperature);
DbgStrOut(L"Wind Direction : %s\n", pszWindDirection);
DbgStrOut(L"Wind Force : %s\n", pszWindForce);
ReleaseTChar(pszUpdateTime);
ReleaseTChar(pszWindDirection);
ReleaseTChar(pszWindForce);
ns1__ArrayOfOneDayWeather *pOneDay = output2.WeatherCast->DayWeather;
std::vector<class ns1__OneDayWeather * >::iterator itDW = pOneDay->OneDayWeather.begin();
//std::vector<ns1__CityInfo * >::iterator it = output.CityInfo.begin();
while (itDW!=pOneDay->OneDayWeather.end())
{
DbgStrOut(L"##########\n");
ns1__OneDayWeather *pDayWeather = *itDW;
DbgStrOut(L"\tDay temp : %.1f\n", pDayWeather->DtTemperature);
DbgStrOut(L"\tDay weather : %d\n", pDayWeather->DtWeatherID);
TCHAR* pszDayWindDirection;
TCHAR* pszDayWindForce;
UTF8ToTChar(pDayWeather->DtWindDirection.c_str(), pszDayWindDirection);
UTF8ToTChar(pDayWeather->DtWindForce.c_str(), pszDayWindForce);
DbgStrOut(L"\tDay Wind Direction : %s\n ", pszDayWindDirection);
DbgStrOut(L"\tDay Wind Force : %s\n", pszDayWindForce);
ReleaseTChar(pszDayWindDirection);
ReleaseTChar(pszDayWindForce);
DbgStrOut(L"\tNight temp : %.1f\n", pDayWeather->NtTemperature);
DbgStrOut(L"\tNight weather : %d\n", pDayWeather->NtWeatherID);
TCHAR* pszNightWindDirection;
TCHAR* pszNightWindForce;
UTF8ToTChar(pDayWeather->NtWindDirection.c_str(), pszNightWindDirection);
UTF8ToTChar(pDayWeather->NtWindForce.c_str(), pszNightWindForce);
DbgStrOut(L"\tNight Wind Direction : %s\n ", pszNightWindDirection);
DbgStrOut(L"\tNight Wind Force : %s\n", pszNightWindForce);
ReleaseTChar(pszNightWindDirection);
ReleaseTChar(pszNightWindForce);
++itDW;
}
}
}
}
return 0;
}
代碼說明
使用Webservice的過程中,我通過查找“閔行”來找到我的城市,再根據城市ID來獲取天氣,其實代碼并不多,多在字符編碼轉換和Debug輸出這部分,因為我們的XML使用UTF-8編碼,而我們的軟件界面通常使用Unicode編碼,所以得轉換,英文的情況下不轉換是沒什么問題的,但漢字一定得轉,否則就是亂碼了。
另外特別注意這個地方:
SSPWeatherServiceSoapProxy gs(SOAP_C_UTFSTRING);
gs.soap_endpoint = "SOAP_C_UTFSTRING,這個是用來指定UTF-8編碼的,一定不能少,下面這個soap_endpoint參數則用來指明這個Webservice的服務地址。
可能的問題以及解決方案
1,超時
程序發布之后有人反映不能使用,Windows Mobile如果用電腦直連的話就沒任何問題,但如果用GPRS上網的話還真的可能出現失敗的情況,我認為這是因為GPRS速度太慢(用起來感覺還不如以前56K貓撥號上網)導致超時的緣故。可以通過下面辦法來解決:
pGS.accept_timeout = 30;
pGS.connect_timeout = 30;
pGS.recv_timeout = 30;
pGS.send_timeout = 30;
pGS.linger_time = 30;
2,無法用WAP方式更新
許多人用手機上網的時候都喜歡用WAP方式,就是那種用代理服務器的方式,移動的接入點叫cmwap,聯通的叫uniwap,代理的地址是“10.0.0.172”。由于連接用了代理,gSOAP也得顯示指定一個代理,方法如下:
pGS->proxy_host = “10.0.0.172”;
pGS->proxy_port = 80;
可能不一定是“10.0.0.172”,端口也可能不是80,這兩個信息可以通過連接管理器相關的API來獲得,具體就不展開了,提示一下,用這個:ConnMgrProviderMessage。
我不知道還有什么疑難雜癥,這篇文章也無法囊括所有內容,大家只能見招拆招了。