Web 服务调用使用 IXMLHttpRequest 不需要的响应文本
Webservice call using IXMLHttpRequest undesired response Text
使用 IXMLHttpRequest 从网络服务获取数据。(实际上,java servlets 文件)。使用以下代码发送请求并从 webservice
获取响应
IXMLHTTPRequestPtr pIXMLHTTPRequest = NULL;
CoInitialize(nullptr);
CATUnicodeString usBuffer;
CATTry
{
hr = pIXMLHTTPRequest.CreateInstance("Msxml2.XMLHTTP.6.0");
if (SUCCEEDED(hr) && (pIXMLHTTPRequest != NULL))
{
hr = pIXMLHTTPRequest->open("POST", "URL", false);
if (SUCCEEDED(hr))
{
CATUnicodeString lsusHeaderName;
lsusHeaderName.Append("Content-Type: application/x-www-form-urlencoded");
lsusHeaderName.Append("Accept: */*");
lsusHeaderName.Append("Login-ticket: " + "ticketvalue");
lsusHeaderName.Append("SecurityContext: " + "securityvalue");
for (int i = 1; i <= ilsusHeaderName.Size(); i++)
{
BSTR bstrHeaderName;
BSTR bstrHeaderValue;
ilsusHeaderName[i].ConvertToBSTR(&bstrHeaderName);
hr = pIXMLHTTPRequest->setRequestHeader(bstrHeaderName, bstrHeaderValue);
}
CATUnicodeString iusRequestParam = "Type=Reference&Name=87333335-00042063&Revision=A.1";
if (iusRequestParam != "")
{
hr = pIXMLHTTPRequest->send(iusRequestParam.ConvertToChar());
}
else
{
hr = pIXMLHTTPRequest->send();
}
{
struct __timeb64 t_stime;
struct __timeb64 t_ctime;
long lProcTime = 0;
memset(&t_stime, 0, sizeof(struct __timeb64));
memset(&t_ctime, 0, sizeof(struct __timeb64));
long nRedyState = READYSTATE_UNINITIALIZED;
_ftime64(&t_stime);
while (nRedyState != READYSTATE_COMPLETE)
{
_ftime64(&t_ctime);
lProcTime = (long)(1000 * (t_ctime.time - t_stime.time) + (t_ctime.millitm - t_stime.millitm));
if (lProcTime > HTTP_TIMEOUT)
{
break;
}
nRedyState = pIXMLHTTPRequest->readyState;
}
}
std::cout << "Request status : " << pIXMLHTTPRequest->status << std::endl;
if (SUCCEEDED(hr) && (pIXMLHTTPRequest->status == 200))
{
_bstr_t spbstrResponse = pIXMLHTTPRequest->responseText;
BSTR bstrString = NULL;
bstrString = spbstrResponse.GetBSTR();
usBuffer.BuildFromBSTR(bstrString);
usOutput = usBuffer;
std::cout << "Output : " << usBuffer << std::endl;
bstrString = NULL;
}
else
{
std::cout << "Failed to send GET/POST Method." << std::endl;
hr = E_FAIL;
}
}
else
{
hr = E_FAIL;
}
}
}
}
CATCatch(CATError, pError)
{
std::cout << "Failed to query XMLHTTP 6.0. Perhaps MSXML 6.0 is not exists." << std::endl;
}
CATEndTry;
这个webservice调用returns200状态码,但是响应文本不是good.Itreturns一些html,java脚本代码作为响应文本。
预期结果是传递对象的属性值之一。示例输出:
{
“PhyID”:“646F916E00005E78609AC53D0000974F”
}
无法在此代码中找到问题,为什么会这样 returns html,javascript code.As 我是 WINAPI 概念的新手,您的帮助非常重要赞赏。
编辑:
通过邮递员检查网络服务,它 returns 预期结果。
Header应该这样传递。这解决了问题。
CATListOfCATUnicodeString lsusHeaderName;
CATListOfCATUnicodeString lsusHeaderValue;
lsusHeaderName.Append("Content-Type");
lsusHeaderValue.Append("application/x-www-form-urlencoded");
lsusHeaderName.Append("Login-ticket");
lsusHeaderValue.Append(usLoginTicket);
lsusHeaderName.Append("SecurityContext");
lsusHeaderValue.Append(usSecurityContext);
使用 IXMLHttpRequest 从网络服务获取数据。(实际上,java servlets 文件)。使用以下代码发送请求并从 webservice
获取响应IXMLHTTPRequestPtr pIXMLHTTPRequest = NULL;
CoInitialize(nullptr);
CATUnicodeString usBuffer;
CATTry
{
hr = pIXMLHTTPRequest.CreateInstance("Msxml2.XMLHTTP.6.0");
if (SUCCEEDED(hr) && (pIXMLHTTPRequest != NULL))
{
hr = pIXMLHTTPRequest->open("POST", "URL", false);
if (SUCCEEDED(hr))
{
CATUnicodeString lsusHeaderName;
lsusHeaderName.Append("Content-Type: application/x-www-form-urlencoded");
lsusHeaderName.Append("Accept: */*");
lsusHeaderName.Append("Login-ticket: " + "ticketvalue");
lsusHeaderName.Append("SecurityContext: " + "securityvalue");
for (int i = 1; i <= ilsusHeaderName.Size(); i++)
{
BSTR bstrHeaderName;
BSTR bstrHeaderValue;
ilsusHeaderName[i].ConvertToBSTR(&bstrHeaderName);
hr = pIXMLHTTPRequest->setRequestHeader(bstrHeaderName, bstrHeaderValue);
}
CATUnicodeString iusRequestParam = "Type=Reference&Name=87333335-00042063&Revision=A.1";
if (iusRequestParam != "")
{
hr = pIXMLHTTPRequest->send(iusRequestParam.ConvertToChar());
}
else
{
hr = pIXMLHTTPRequest->send();
}
{
struct __timeb64 t_stime;
struct __timeb64 t_ctime;
long lProcTime = 0;
memset(&t_stime, 0, sizeof(struct __timeb64));
memset(&t_ctime, 0, sizeof(struct __timeb64));
long nRedyState = READYSTATE_UNINITIALIZED;
_ftime64(&t_stime);
while (nRedyState != READYSTATE_COMPLETE)
{
_ftime64(&t_ctime);
lProcTime = (long)(1000 * (t_ctime.time - t_stime.time) + (t_ctime.millitm - t_stime.millitm));
if (lProcTime > HTTP_TIMEOUT)
{
break;
}
nRedyState = pIXMLHTTPRequest->readyState;
}
}
std::cout << "Request status : " << pIXMLHTTPRequest->status << std::endl;
if (SUCCEEDED(hr) && (pIXMLHTTPRequest->status == 200))
{
_bstr_t spbstrResponse = pIXMLHTTPRequest->responseText;
BSTR bstrString = NULL;
bstrString = spbstrResponse.GetBSTR();
usBuffer.BuildFromBSTR(bstrString);
usOutput = usBuffer;
std::cout << "Output : " << usBuffer << std::endl;
bstrString = NULL;
}
else
{
std::cout << "Failed to send GET/POST Method." << std::endl;
hr = E_FAIL;
}
}
else
{
hr = E_FAIL;
}
}
}
}
CATCatch(CATError, pError)
{
std::cout << "Failed to query XMLHTTP 6.0. Perhaps MSXML 6.0 is not exists." << std::endl;
}
CATEndTry;
这个webservice调用returns200状态码,但是响应文本不是good.Itreturns一些html,java脚本代码作为响应文本。
预期结果是传递对象的属性值之一。示例输出:
{ “PhyID”:“646F916E00005E78609AC53D0000974F” }
无法在此代码中找到问题,为什么会这样 returns html,javascript code.As 我是 WINAPI 概念的新手,您的帮助非常重要赞赏。
编辑: 通过邮递员检查网络服务,它 returns 预期结果。
Header应该这样传递。这解决了问题。
CATListOfCATUnicodeString lsusHeaderName;
CATListOfCATUnicodeString lsusHeaderValue;
lsusHeaderName.Append("Content-Type");
lsusHeaderValue.Append("application/x-www-form-urlencoded");
lsusHeaderName.Append("Login-ticket");
lsusHeaderValue.Append(usLoginTicket);
lsusHeaderName.Append("SecurityContext");
lsusHeaderValue.Append(usSecurityContext);