c++写的http程序,unicode下对方接受参数值为null

other123othe 2014-08-29 12:29:35


//以下是post

// CString strErrorMsg;
// CString strFileName(L"d:\\test.log");
CString strResponsePage(L"d:\\test.log");
strResponsePage += ".log";

int nResult = 5;

CString strErrorMsg;
CString strURL;
GetDlgItem(IDC_EDIT1)->GetWindowText(strURL);
CString strParamName;
GetDlgItem(IDC_EDIT2)->GetWindowText(strParamName);
CString strParamValue;
GetDlgItem(IDC_EDIT3)->GetWindowText(strParamValue);
CString strParamName1;
GetDlgItem(IDC_EDIT5)->GetWindowText(strParamName1);
CString strParamValue1;
GetDlgItem(IDC_EDIT6)->GetWindowText(strParamValue1);
CString strParamName2;
GetDlgItem(IDC_EDIT7)->GetWindowText(strParamName2);
CString strParamValue2;
GetDlgItem(IDC_EDIT8)->GetWindowText(strParamValue2);

CString strParam;



// if(!strParamName.IsEmpty() && !strParamValue.IsEmpty())
// {
// strParam = strParamName;
// strParam += "=";
// strParam += strParamValue;
// strParam += "&";
// strParam += strParamName1;
// strParam += "=";
// strParam += strParamValue1;
// strParam += "&";
// strParam += strParamName2;
// strParam += "=";
// strParam += strParamValue2;
// }
// else
// {
strParam = "idnumber=test";
/* }*/
if(!strURL.IsEmpty() && !strParam.IsEmpty())
{
int nResult = 5;
CInternetSession session(_T("HttpFilePOSTer"));
CHttpConnection* pServer = NULL;
CHttpFile* pFile = NULL;
TCHAR szBuff[1024];
DWORD dwRet;
CFile file;
// http://192.168.0.155:8080/UploadFile/servlet/strTest?name=123&birthday=20140827&address=456
DWORD dwServiceType;
CString strServer, strObject, strUsername, strPassword;
INTERNET_PORT nPort;

if (AfxParseURLEx(strURL, dwServiceType, strServer, strObject, nPort, strUsername, strPassword) == FALSE)
{
strErrorMsg = "地址错误";
nResult = 1;
return ;
}

try
{
pServer = session.GetHttpConnection(strServer, nPort);
CString objName(L"/translate_txt");
pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST, strObject);//_T("/uploadfilepj/newfile.txt"));
//pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST, objName);
TRACE(strObject);TRACE(_T("\n"));
CString szHeaders = L"Content-Type: application/x-www-form-urlencoded;";

pFile->AddRequestHeaders(szHeaders);
CString strHead(L"Accept: */*");
pFile->AddRequestHeaders(strHead);
BOOL res = pFile->SendRequest(szHeaders,szHeaders.GetLength(),(LPVOID)(LPCTSTR)strParam,strParam.GetLength());
//BOOL res = pFile->SendRequest(NULL,0,(LPVOID)(LPCTSTR)strParam,strParam.GetLength());
if(res == TRUE)
{
// AfxMessageBox()
}
else
{
return;

}

pFile->EndRequest();
pFile->QueryInfoStatusCode(dwRet);
if(dwRet == HTTP_STATUS_OK)
{
strErrorMsg = "提交成功";
AfxMessageBox(_T("oK,成功"));
}
else
{
strErrorMsg = "提交失败";
AfxMessageBox(_T("oK,失败"));
}
}
catch (CInternetException* pEx)
{
nResult = 1;
pEx->GetErrorMessage(szBuff, 1024);
strErrorMsg = szBuff;
}

if(pFile)
{
delete pFile;
pFile = NULL;
}
if(pServer)
{
delete pServer ;
pServer = NULL;
}
}



编码方式为_MBCS 则对方可以成功接受传输的参数值为正确的传输的值
如果我的工程编码方式为 unicode,则对方java服务器接受的参数的值为null。现在是整个大的工程就是unicode的编码,不可能修改的,但是不修改又无法传输。在unicode编码方式下 pFile->QueryInfoStatusCode(dwRet);得到的dwRet 是HTTP_STATUS_OK。但是对方就是接受的为null
...全文
103 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
ulwfnh1100 2014-08-29
  • 打赏
  • 举报
回复
使用API函数WideCharToMultiByte,把UNICODE字符转为多字节字符传输就可以了
猞猁狲 2014-08-29
  • 打赏
  • 举报
回复
szHeaders 这个http头里是不是得指定下编码格式,找找吧

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧