Win95+VC6+IE4+MSXML3时的XMLHTTP解决方法

首先,在Win95+VC6+IE4+MSXML3下,使用MSXML3的XMLHTTP在open时会发生错误(抛出异常,异常码为EFAIL,异常描述为空),个人认为是wininet和urlmon的问题(本人并未深入确认,本想安装IE5/5.5再测试,但无奈该版本太古老,找不到安装包,只能找到所谓绿色版)。

既然MSXML3的XMLHTTP无法使用,只好使用CInternetSession自行编写了:

void Send(Joytech::MSXML3::IXMLDOMDocument documentSend, const CString& url, const CString& soapAction, Joytech::MSXML3::IXMLDOMDocument& documentRecv) {     static LPCTSTR header = _T("Accept: */*\r\n\ Accept-Language: zh-cn\r\n\ X-Requested-With: TWenXMLHttpRequest\r\n\ Content-Type: text/xml; charset=UTF-8\r\n\ UA-CPU: x86\r\n\ Accept-Encoding: gzip, deflate\r\n\ SOAPAction: ");

 CString strHeader = header + soapAction + _T("\r\n");  DWORD dwServiceType;  CString strServer, strObject;  INTERNET_PORT nPort = 80;

 //vc6版本的AfxParseURL不支持INTERNET_SERVICE_HTTPS  if (!AfxParseURL(url, dwServiceType, strServer, strObject, nPort) || (dwServiceType != INTERNET_SERVICE_HTTP))   throw new Joytech::Exception::CJoytechException(_T("url中的协议出错!"));

 CString xml = documentSend.GetXml();     CInternetSession session;  CHttpConnection* pConnection = session.GetHttpConnection(strServer, nPort);  CHttpFile* pFile = pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, strObject);  pFile->SendRequest(strHeader, (LPVOID)(LPCTSTR)xml, xml.GetLength() * sizeof(TCHAR));    DWORD dwRet;  pFile->QueryInfoStatusCode(dwRet);  if(dwRet != HTTP_STATUS_OK) throw new Joytech::Exception::CJoytechException(_T("XMLHTTP状态出错!"));

 CString strXML;  int nSize = pFile->GetLength();  {   CString strRead;   while(pFile->ReadString(strRead)) strXML += strRead;  }    pFile->Close();     delete pFile;  pConnection->Close();     delete pConnection;     session.Close();

 Joytech::MSXML3::CreateXMLDocument(documentRecv);  documentRecv.loadXML(Joytech::StringConvert::Joytech_UTF82T((LPCSTR)strXML));//strXML虽然是CString类型,但内部的数据其实是UTF8(依赖于WebService服务器的xml序列化编码)的格式,因此需要手动转换为LPCTSTR格式 }

其中有两点需要注意:

1. 如果要支持https等协议,必须自行编写URL解析函数,因为VC6版本的URL解析函数AfxParseURL仅支持INTERNET_SERVICE_URL、INTERNET_SERVICE_FTP、INTERNET_SERVICE_GOPHER和INTERNET_SERVICE_HTTP四种。

2. 对于从服务器端返回的数据,虽然在代码中是使用pFile->ReadString取得的,并且是CString类型,但并不能简单地将之作为CString处理;一定要注意到,返回的xml文本是有编码的,一般为utf8!并且,在转换编码为LPCTSTR时,需要注意,Win95下的API函数MultiByteToWideChar/WideCharToMultiByte不支持UTF8,因此需要自己编写。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
主  题: 在VC中使用XMLHTTP,怎样初始化,怎样建立连接?在线等。 我的一段代码在.NET中用C#已经调通,我想搬到VC++ 6.0上,但不知道任何各种写法规则。 C# 中为: MSXML2.XMLHTTP xmlHttp_ = new XMLHTTP(); xmlHttp_.open("PROPFIND", serverUrl, false, username, password); xmlHttp_.send(null); 放到VC++ 6.0 中应为什么? 以下是我写的,但运行出错。 #import "msxml4.dll" using namespace MSXML2; HRESULT hr; //MSXML2::IXMLHTTPRequest pIXMLHttpRequest; CString serverUrl ="http://services.msn.com/svcs/hotmail/httpmail.asp"; MSXML2::IXMLHTTPRequestPtr pIXMLHttpRequest; pIXMLHttpRequest.CreateInstance("Msxml2.XMLHTTP.4.0"); if (pIXMLHttpRequest==NULL) AfxMessageBox("pIXMLHttpRequest error"); try { // Create XMLHttpRequest object and initialize pIXMLHttpRequest. hr = pIXMLHttpRequest->open(_bstr_t(_T("PROPFIND")), _bstr_t(_T(serverUrl)), _variant_t(VARIANT_FALSE), _variant_t("test5_12"), _variant_t("1234567")); if(SUCCEEDED(hr)) ::MessageBox(NULL, _T("Success !"), _T(""), MB_OK); } catch(...) { //DisplayErrorToUser(); AfxMessageBox("error"); } 在调用方法open的候出错! 回复人: masterz(www.fruitfruit.com) ( ) 信誉:273 2003-06-10 21:34:02Z 得分:20 ? #import "msxml.dll" #import "msxml2.dll" using namespace MSXML2; int main(int argc, char* argv[]) { printf("Test of XMLHTTP by masterz!\n"); CoInitialize(NULL); try { IXMLHTTPRequestPtr xmlrequest; xmlrequest.CreateInstance("Msxml2.XMLHTTP"); _variant_t varp(false); xmlrequest->open(_bstr_t("GET"),_bstr_t("http://www.csdn.net/expert/topic/855/855052.xml?temp=.176037"),varp); xmlrequest->send(); BSTR bstrbody; xmlrequest->get_responseText(&bstrbody); _bstr_t bstrtbody(bstrbody); printf("%s\n",(LPCTSTR)bstrtbody); } catch (_com_error &e) { printf("Description = '%s'\n", (char*) e.Description()); } CoUninitialize(); printf("program end\n"); return 0; } ///////Post with XMLHTTP/////////////////////// #import "msxml.dll" #import "msxml2.dll" #include "Atlbase.h" using namespace MSXML2; int main(int argc, char* argv[]) { printf("Test of XMLHTTP by masterz!\n"); CoInitialize(NULL); try { IXMLHTTPRequestPtr xmlrequest; //xmlrequest.CreateInstance("Msxml2.XMLHTTP"); xmlrequest.CreateInstance(__uuidof(XMLHTTP)); CComVariant vFalse(FALSE); CComVariant vNull(NULL); xmlrequest->open("POST", _bstr_t("http://211.157.102.21/member/logon.asp"),vFalse,vNull,vNull); xmlrequest->setRequestHeader("Content-Type:","application/x-www-form-urlencoded"); _bstr_t bsdata("name=xxx&pass=xxx&type=1");//use your login name and password xmlrequest->send(_variant_t(bsdata)); BSTR bstrbody; xmlrequest->get_responseText(&bstrbody); _bstr_t bstrtbody(bstrbody); printf("%s\n",(LPCTSTR)bstrtbody); //MessageBox(0,bstrtbody,"",MB_OK); } catch (_com_error &e) { printf("Description = '%s'\n", (char*) e.Description()); } CoUninitialize(); printf("program end\n"); return 0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值