急!CHttpFile接收不到返回消息?

sikivitas 2011-05-18 01:49:50
用CHttpFile向服务器上传文件,服务器显示已经成功上传,并且发送了返回信息,但是CHttpFile就是收不到返回信息
以下为源码:


USES_CONVERSION;
CInternetSession Session;
CHttpConnection *pHttpConnection = NULL;
INTERNET_PORT nPort = 8080;
CFile fTrack;
CHttpFile* pHTTP;
CString strHTTPBoundary = _T("BSQHttpBoundary");
CString strPreFileData;
CString strPostFileData;
DWORD dwTotalRequestLength;
DWORD dwChunkLength;
DWORD dwReadLength;
DWORD dwResponseLength;
TCHAR szError[MAX_PATH];
void* pBuf;
LPSTR szResponse;
CString strResponse;
BOOL blnSuccess = TRUE;
CString strDebugMessage;


if (FALSE == fTrack.Open(strFilePath, CFile::modeRead | CFile::shareDenyWrite))
{
AfxMessageBox(_T("Unable to open the file."));
return FALSE;
}

CString strFileName = strFilePath.Mid(strFilePath.ReverseFind('\\') + 1);
strPreFileData = MakePreFileData(strHTTPBoundary, strFileName);
strPostFileData = MakePostFileData(strHTTPBoundary);
dwTotalRequestLength = strPreFileData.GetLength() + strPostFileData.GetLength() + fTrack.GetLength();

dwChunkLength = 64 * 1024;

pBuf = malloc(dwChunkLength);
if (NULL == pBuf)
return FALSE;

try
{
pHttpConnection = Session.GetHttpConnection(defServerName,nPort);
pHTTP = pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, defObjectName);
pHTTP->AddRequestHeaders(MakeRequestHeaders(strHTTPBoundary));//"Content-Type: multipart/form-data; boundary=%s\r\n"
pHTTP->SendRequestEx(dwTotalRequestLength, HSR_SYNC | HSR_INITIATE);

#ifdef _UNICODE
pHTTP->Write(W2A(strPreFileData), strPreFileData.GetLength());
#else
pHTTP->Write((LPSTR)(LPCSTR)strPreFileData, strPreFileData.GetLength());
#endif

dwReadLength = -1;
while (0 != dwReadLength)
{
strDebugMessage.Format(_T("%u / %u\n"), fTrack.GetPosition(), fTrack.GetLength());
TRACE(strDebugMessage);
dwReadLength = fTrack.Read(pBuf, dwChunkLength);
if (0 != dwReadLength)
pHTTP->Write(pBuf, dwReadLength);
}

#ifdef _UNICODE
pHTTP->Write(W2A(strPostFileData), strPostFileData.GetLength());
#else
pHTTP->Write((LPSTR)(LPCSTR)strPostFileData, strPostFileData.GetLength());
#endif

pHTTP->EndRequest(HSR_SYNC);

dwResponseLength = pHTTP->GetLength(); <-- 这里一直都是“0”
while (0 != dwResponseLength)
{
szResponse = (LPSTR)malloc(dwResponseLength + 1);
szResponse[dwResponseLength] = '\0';
pHTTP->Read(szResponse, dwResponseLength);
strResponse += szResponse;
free(szResponse);
dwResponseLength = pHTTP->GetLength();
}

AfxMessageBox(strResponse);

}

catch (CException* e)
{
e->GetErrorMessage(szError, MAX_PATH);
e->Delete();
AfxMessageBox(szError);
blnSuccess = FALSE;
}

pHTTP->Close();
delete pHTTP;

fTrack.Close();

if (NULL != pBuf)
{
free(pBuf);
}

return blnSuccess;
...全文
139 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
sikivitas 2011-05-19
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 visualeleven 的回复:]
CHttpFile::QueryInfo()
CHttpFile::QueryInfoStatusCode();
查询一下信息和状态码
[/Quote]

用QueryInfo查询出的返回信息长度一直是0,内容是空的
QueryInfoStatusCode返回的是200,正常的


奇怪的是,用asp.net写的服务器就能收到正常信息,而那个java就是收不到
asp写的内容为:

<%
Response.ContentType = "text/html;charset=GBK"
Response.CacheControl = "no-cache"
If Request.Form("recordid") <> "" Then
Response.Write(1)
Else
Response.Write(2)
End If
response.End()
%>
sikivitas 2011-05-19
  • 打赏
  • 举报
回复
服务器用的是java,下面是片段:


private void writeXmlToResp(HttpServletResponse resp,String xml)
{
try{
resp.setContentType("text/html;charset=GBK");
resp.setHeader("Cache-Control","no-cache");
PrintWriter out=resp.getWriter();
out.write(xml);
out.flush();
out.close();
}catch(Exception e){e.printStackTrace();}
}
sikivitas 2011-05-19
  • 打赏
  • 举报
回复
查询状态都是正常的,而且服务器也都收到了
Eleven 2011-05-19
  • 打赏
  • 举报
回复
CHttpFile::QueryInfo()
CHttpFile::QueryInfoStatusCode();
查询一下信息和状态码
sikivitas 2011-05-19
  • 打赏
  • 举报
回复
达人在哪里~~~~
sikivitas 2011-05-18
  • 打赏
  • 举报
回复
救命呀~~ 谁知道呀~~
sikivitas 2011-05-18
  • 打赏
  • 举报
回复
没人知道吗??? 我哭~~~

18,356

社区成员

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

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