mycsdn12306 2023-11-08 19:45 采纳率: 100%
浏览 11
已结题

MFC 调用HTTP接口问题

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("&lt;WS_EOL_DATA_UPLOAD xmlns=\"http://device.service.moresoft.com/\"&gt; "));
    strBody.AppendFormat(_T("&lt;M_MACHINE_NO&gt;%s&lt;/M_MACHINE_NO&gt;   "), L"01");
    strBody.AppendFormat(_T("&lt;M_WORKSTATION_SN&gt;%s&lt;/M_WORKSTATION_SN&gt;   "), L"SN01");
    strBody.AppendFormat(_T("&lt;M_EMP_NO&gt;%s&lt;/M_EMP_NO&gt;   "), L"E01");
    strBody.AppendFormat(_T("&lt;M_MO&gt;%s&lt;/M_MO&gt; "), L"005");
    strBody.AppendFormat(_T("&lt;M_OPERATION&gt;%s&lt;/M_OPERATION&gt; "), L"25");
    strBody.AppendFormat(_T("&lt;M_SATGE&gt;%s&lt;/M_SATGE&gt; "), L"0");
    strBody.AppendFormat(_T("&lt;M_TEST_MODE&gt;%s&lt;/M_TEST_MODE&gt; "), L"N");
    strBody.AppendFormat(_T("&lt;M_PRODUCT_SN&gt;%s&lt;/M_PRODUCT_SN&gt; "), L"01");
    strBody.AppendFormat(_T("&lt;M_CELL_SN&gt;%s&lt;/M_CELL_SN&gt; "), L"01");
    strBody.AppendFormat(_T("&lt;M_QTY&gt;%s&lt;/M_QTY&gt; "), L"1");
    strBody.AppendFormat(_T("&lt;M_NG_QTY&gt;%s&lt;/M_NG_QTY&gt; "), L"0");
    strBody.AppendFormat(_T("&lt;M_RESULT&gt;%s&lt;/M_RESULT&gt;  "), L"T");
    strBody.AppendFormat(_T("&lt;M_ERROR&gt;%s&lt;/M_ERROR&gt;  "), L"");
    strBody.AppendFormat(_T("&lt;M_ERROR_QTY&gt;%s&lt;/M_ERROR_QTY&gt;  "), L"");
    strBody.AppendFormat(_T("&lt;M_ERROR_POINT&gt;%s&lt;/M_ERROR_POINT&gt;  "), L"");
    strBody.AppendFormat(_T("&lt;M_ITEMVALUE&gt;%s&lt;/M_ITEMVALUE&gt;  "), L"Vol:3316");
    strBody.AppendFormat(_T("&lt;M_VOLTAGE&gt;%s&lt;/M_VOLTAGE&gt;  "), L"3316");
    strBody.AppendFormat(_T("&lt;Param1&gt;%s&lt;/Param1&gt;  "), L"");
    strBody.AppendFormat(_T("&lt;Param2&gt;%s&lt;/Param2&gt;  "), L"");
    strBody.Append(_T("&lt;/WS_EOL_DATA_UPLOAD&gt;  "));
    strBody.Append(_T("&lt;/soap:Body&gt;  "));
    strBody.Append(_T("&lt;/soap:Envelope&gt;  "));

#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();
}
  • 写回答

13条回答 默认 最新

  • 社区专家-Monster-XH 2023-11-08 19:45
    关注

    基于Monster 组和AIGC的调写:若有帮助,还望采纳~~:

    • 出现“请求格式无效: text/xml; charset=utf-8”这样的错误通常是因为HTTP请求的内容类型(Content-Type)头或者请求体的格式有问题。从代码来看,有几个潜在的问题点:
    1. Content-Type头部:已经将Content-Type设置为"text/xml; charset=utf-8",这通常是正确的,要确保服务器接受这种格式的内容。
    2. XML格式:XML请求体用了HTML实体(如&lt;&gt;代替了<>)。这可能导致服务器无法正确解析XML,因为它看起来是被编码过的。
    3. 字符编码:代码中有一部分转换了内容到UTF-8编码,确保服务器是期望接收UTF-8编码的XML。

    解决这个问题的步骤可能包括:

    • 确认服务器端的API文档,查看正确的Content-Type头部和请求体格式。
    • 将XML字符串的HTML实体转换回正常的XML标签。
    • 检查服务器是否确实接受UTF-8编码的文本。

    以下是对代码进行的一些修改建议:

    • 不要用HTML实体来编写XML内容。直接写标准的XML标签。
    • 检查你是否已经正确设置了HTTP请求头。
    • 确保XML内容格式与服务器端所期待的一致。
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(12条)

报告相同问题?

问题事件

  • 系统已结题 11月17日
  • 已采纳回答 11月9日
  • 创建了问题 11月8日

悬赏问题

  • ¥15 远程访问linux主机超时
  • ¥15 odoo17存货管理优势于中国国内该行业传统ERP或MES的详细解读和举例
  • ¥15 CPU卡指令整合指令数据都在图片上
  • ¥15 火车票关联12306问题
  • ¥15 odoo17处理受托加工产品
  • ¥15 如何用MATLAB编码图三的积分
  • ¥15 圆孔衍射光强随孔径变化
  • ¥15 MacBook pro m3max上用vscode运行c语言没有反应
  • ¥15 ESP-PROG配置错误,ALL ONES
  • ¥15 结构功能耦合指标计算