1、CString m_strHttp = _T(“http:/ m_strObject = _T(“/TranslateService.asmx“);/本文以调用 http:/ 中的 /Translate 方法为例说明如何调用 WebService 中的方法/UTF8 编码、解码 .由于 webservice 中返回的默认的数据类型是 utf8 编码格式的,/所以本人将其转换成 Unicode 编码格式,以便于程序查看./编码/Convert(str,936,CP_UTF8); /解码/Convert(str,CP_UTF8,936);/编码、解码函数CString CWebService:Co
2、nvert(CString strInput, int nSourceCodepage, int nTargetCodepage)int len = strInput.GetLength(); wchar_t* pUnicode = NULL; int unicodeLen = MultiByteToWideChar(nSourceCodepage,0,(const char *)strInput.GetBuffer(0),-1,NULL,0); pUnicode=new wchar_tunicodeLen+1; memset(pUnicode,0,(unicodeLen+1)*sizeof(
3、wchar_t); MultiByteToWideChar(nSourceCodepage,0,(const char *)strInput.GetBuffer(0),-1,(LPWSTR)pUnicode,unicodeLen); BYTE* pTargetData = NULL; int targetLen=WideCharToMultiByte(nTargetCodepage,0,(LPWSTR)pUnicode,-1,(char *)pTargetData,0,NULL,NULL); pTargetData=new BYTEtargetLen+1; memset(pTargetData
4、,0,targetLen+1); WideCharToMultiByte(nTargetCodepage,0,(LPWSTR)pUnicode,-1,(char *)pTargetData,targetLen,NULL,NULL); CString strTarget; strTarget.Format(“%s“,pTargetData); delete pUnicode; delete pTargetData; return strTarget; DWORD dwHttpRequestFlags =INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_
5、NO_AUTO_REDIRECT;const TCHAR szHeaders =_T(“Accept: text/*rnUser-Agent: MFC_WebSevice_Samplern“);/GetWebServiceObject:获取 webservice 方法的返回的数据/strURL 包函了 WebService 的调用地址和方法,以及传递给方法的参数/strObjectType 为 WebService 返回的数据类型,本文是 string (字符串)类型CString CWebService:GetWebServiceObject(CString strURL, CString
6、strObjectType)CString strList;CString strXMLObject;LPCTSTR pszURL = strURL;BOOL bStripMode = FALSE;CHttpFile *pFile = NULL;CInternetSession netSession;CHttpConnection* pServer = NULL;tryCString strServerName;CString strObject;INTERNET_PORT nPort;DWORD dwServiceType;if (!AfxParseURL(pszURL, dwService
7、Type, strServerName, strObject, nPort) |dwServiceType != INTERNET_SERVICE_HTTP)THROW(1);pServer = netSession.GetHttpConnection(strServerName, nPort);pFile = pServer-OpenRequest(CHttpConnection:HTTP_VERB_GET,strObject, NULL, 1, NULL, NULL, dwHttpRequestFlags);pFile-AddRequestHeaders(szHeaders);pFile-
8、SendRequest();DWORD dwRet;pFile-QueryInfoStatusCode(dwRet);if (dwRet = HTTP_STATUS_DENIED)/需要用户名密码DWORD dwPrompt;dwPrompt = pFile-ErrorDlg(NULL, ERROR_INTERNET_INCORRECT_PASSWORD,FLAGS_ERROR_UI_FLAGS_GENERATE_DATA | FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS, NULL);if (dwPrompt != ERROR_INTERNET_FORCE_RETR
9、Y)THROW (1);pFile-SendRequest();pFile-QueryInfoStatusCode(dwRet);if (dwRet = HTTP_STATUS_MOVED |dwRet = HTTP_STATUS_REDIRECT |dwRet = HTTP_STATUS_REDIRECT_METHOD)/需要重定向CString strNewLocation;pFile-QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF, strNewLocation);int nPlace = strNewLocation.Find(_T(“Location: “
10、);if (nPlace = -1)THROW (2);strNewLocation = strNewLocation.Mid(nPlace + 10);nPlace = strNewLocation.Find(n);if (nPlace 0)strNewLocation = strNewLocation.Left(nPlace);pFile-Close();delete pFile;pServer-Close();delete pServer;/ figure out what the old place wasif (!AfxParseURL(strNewLocation, dwServi
11、ceType, strServerName, strObject, nPort)THROW (2);if (dwServiceType != INTERNET_SERVICE_HTTP)THROW (2);/ try again at the new locationpServer = netSession.GetHttpConnection(strServerName, nPort);pFile = pServer-OpenRequest(CHttpConnection:HTTP_VERB_GET,strObject, NULL, 1, NULL, NULL, dwHttpRequestFl
12、ags);pFile-AddRequestHeaders(szHeaders);pFile-SendRequest();pFile-QueryInfoStatusCode(dwRet);if (dwRet != HTTP_STATUS_OK)THROW(2);return _T(“);CString strRead;int nRow = 0;strList.Empty();int nSize = pFile-GetLength();while(pFile-ReadString(strRead)int nNum = strRead.GetLength();strRead = Convert(st
13、rRead,CP_UTF8,936);strList += strRead;catch(CInternetException* pEx)TCHAR szErr1024;pEx-GetErrorMessage(szErr, 1024);int nRetCode = 2;pEx-Delete();CString strMsg;strMsg.Format(“%s“,szErr);CEBookDlg* pEBookWnd = (CEBookDlg*)AfxGetMainWnd();return strMsg;if (pFile != NULL)delete pFile;pFile = NULL;if
14、(pServer != NULL)delete pServer;pServer = NULL;netSession.Close();/将 XML 对象解析出来int nObj = strList.Find(strObjectType);int nLeft = strList.Find(“,nObj);int nRight = strList.Find(“,nLeft);strXMLObject = strList.Mid(nLeft + 1,nRight - nLeft - 1);/return strXMLObject;/调用 WebService 中的方法void Translate(CString strLan,CString strText)CString strRUL;strRUL.Format(“%s/Translate?LanguageMode=%s/获取 webService 对应方法的返回数据,本例返回的是 string 类型数据CString strVal = GetWebServiceObject(strRUL,_T(“string“);