MFC,使用CInternetSession,CHttpConnection,CHttpFile调用HTTP接口,POST上传,返回“请求格式无效: text/xml; charset=utf-8。”,不知道哪里错了。
CStringW AnsiToWideString(const char* pChar , DWORD code)
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/349847344996160.png "#left")
{
CStringW sTmpW;
sTmpW.Empty();
if (pChar == NULL)
return sTmpW;
wchar_t* pszBuf = NULL;
int needWChar = MultiByteToWideChar(code, 0, pChar, -1, NULL, 0);
if (needWChar > 0)
{
pszBuf = new wchar_t[needWChar + 1];
ZeroMemory(pszBuf, (needWChar + 1) * sizeof(wchar_t));
MultiByteToWideChar(code, 0, pChar, -1, pszBuf, needWChar);
sTmpW = pszBuf;
delete []pszBuf;
}
return sTmpW;
}
void CTestDlg::OnBnClickedButton1()
{
CString strServerName;
CString strObject;
INTERNET_PORT nPort;
DWORD dwServiceType;
CString strUrl = L"http://172.18.3.67:8020/MesFrameWork.asmx/WS_EOL_DATA_UPLOAD";
AfxParseURL((LPCTSTR)strUrl, dwServiceType, strServerName, strObject, nPort);
CInternetSession internetSession;
CHttpConnection* pHttpConnect = NULL;
CHttpFile* pHttpFile = NULL;
pHttpConnect = internetSession.GetHttpConnection(strServerName, nPort);
pHttpFile = pHttpConnect->OpenRequest(CHttpConnection::HTTP_VERB_POST, strObject); //这里会在头部加入POST字段.
CString strHeaders;
CString strBody(L"");
strBody.Append(_T("<?xml version=\"1.0\" encoding=\"utf-8\"?> "));
strBody.Append(_T("<soap:Envelope "));
strBody.Append(_T("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "));
strBody.Append(_T("xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "));
strBody.Append(_T("xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"> "));
strBody.Append(_T("<soap:Body> "));
strBody.Append(_T("<WS_EOL_DATA_UPLOAD xmlns=\"http://device.service.moresoft.com/\"> "));
strBody.AppendFormat(_T("<M_MACHINE_NO>%s</M_MACHINE_NO> "), L"01");
strBody.AppendFormat(_T("<M_WORKSTATION_SN>%s</M_WORKSTATION_SN> "), L"SN01");
strBody.AppendFormat(_T("<M_EMP_NO>%s</M_EMP_NO> "), L"E01");
strBody.AppendFormat(_T("<M_MO>%s</M_MO> "), L"005");
strBody.AppendFormat(_T("<M_OPERATION>%s</M_OPERATION> "), L"25");
strBody.AppendFormat(_T("<M_SATGE>%s</M_SATGE> "), L"0");
strBody.AppendFormat(_T("<M_TEST_MODE>%s</M_TEST_MODE> "), L"N");
strBody.AppendFormat(_T("<M_PRODUCT_SN>%s</M_PRODUCT_SN> "), L"01");
strBody.AppendFormat(_T("<M_CELL_SN>%s</M_CELL_SN> "), L"01");
strBody.AppendFormat(_T("<M_QTY>%s</M_QTY> "), L"1");
strBody.AppendFormat(_T("<M_NG_QTY>%s</M_NG_QTY> "), L"0");
strBody.AppendFormat(_T("<M_RESULT>%s</M_RESULT> "), L"T");
strBody.AppendFormat(_T("<M_ERROR>%s</M_ERROR> "), L"");
strBody.AppendFormat(_T("<M_ERROR_QTY>%s</M_ERROR_QTY> "), L"");
strBody.AppendFormat(_T("<M_ERROR_POINT>%s</M_ERROR_POINT> "), L"");
strBody.AppendFormat(_T("<M_ITEMVALUE>%s</M_ITEMVALUE> "), L"Vol:3316");
strBody.AppendFormat(_T("<M_VOLTAGE>%s</M_VOLTAGE> "), L"3316");
strBody.AppendFormat(_T("<Param1>%s</Param1> "), L"");
strBody.AppendFormat(_T("<Param2>%s</Param2> "), L"");
strBody.Append(_T("</WS_EOL_DATA_UPLOAD> "));
strBody.Append(_T("</soap:Body> "));
strBody.Append(_T("</soap:Envelope> "));
#ifdef _UNICODE
CStringA sBody_utf8 = WideToAnsiString((LPTSTR)(LPCTSTR)strBody, CP_UTF8);
#else
CStringA = strBody ;
#endif
strHeaders.Append(_T("Content-Type: text/xml; charset=utf-8 \r\n"));
strHeaders.AppendFormat(_T("Content-Length: %d \r\n"),sBody_utf8.GetLength());
pHttpFile->AddRequestHeaders(strHeaders, HTTP_ADDREQ_FLAG_COALESCE | HTTP_ADDREQ_FLAG_ADD_IF_NEW) ;
pHttpFile->SendRequestEx(sBody_utf8.GetLength(), HSR_SYNC | HSR_INITIATE);
pHttpFile->Write(sBody_utf8, sBody_utf8.GetLength());
pHttpFile->EndRequest(HSR_SYNC);
DWORD dwHttpStatus;
if (!pHttpFile->QueryInfoStatusCode(dwHttpStatus))
{
AfxMessageBox(L"无返回:QueryInfoStatusCode()");
pHttpFile->Close();
pHttpConnect->Close();
internetSession.Close();
}
DWORD dwResponseLength = pHttpFile->GetLength();
CString strRt;
while (0 != dwResponseLength)
{
char* szResponse = new char[dwResponseLength + 1];
szResponse[dwResponseLength] = 0;
pHttpFile->Read(szResponse, dwResponseLength);
strRt += AnsiToWideString(szResponse, CP_UTF8) ; // 结果---》unicode
delete[] szResponse;
dwResponseLength = pHttpFile->GetLength();
}
AfxMessageBox(strRt);// 这里返回的是 "请求格式无效: text/xml; charset=utf-8。"
pHttpFile->Close();
pHttpConnect->Close();
internetSession.Close();
}