我的代码与供应商提供的一个小型 Java 应用程序对话。这个 Java 应用程序在 localhost:57000 设置了一个 Web 服务器,用于控制“机器”的状态。出于这个问题的目的,我需要将“机器”的状态从“关闭”更改为“开启”。为了实现这一点,我应该将以下字符串 HTTP PUT 到http://localhost:57000/settings.xml的“机器” :
<settings><machine_state><status>on</status></machine_state></settings>
此 Curl 命令完美运行:
curl -X PUT -H "Content-Type:application/xml" -d @settings.xml http://localhost:57000/settings.xml "
其中本地文件“settings.xml”中包含上述 xml 字符串。
我想做 Curl 对 MFC 的 WININET 类所做的事情。恕我直言,以下代码应该与 curl 所做的完全相同。遗憾的是,尽管 localhost Web 服务器返回代码 200,但它忽略了我的 xml 字符串。我错过了什么小东西?
int MyHttp::HttpPutThread() NOTHROW
{
try {
m_xml = "<settings><machine_state><status>on</status></machine_state></settings>";
m_url = "settings.xml"
CInternetSession session;
SetSessionOptions(session);
CString server = "localhost:57920";
boost::scoped_ptr<CHttpConnection> phttp(session.GetHttpConnection(server));
LPCTSTR accept = 0;//"text/xml";
boost::scoped_ptr<CHttpFile> phttpfile(phttp->OpenRequest(
"PUT", //verb
"settings.xml", //object name
0, //referer
1, //context
&accept, // accept types
0, //version
INTERNET_FLAG_EXISTING_CONNECT));
CString header = "Content-Type:application/xml\r\n";
if(phttpfile->SendRequest(header,(LPVOID)m_xml.GetBuffer(), m_xml.GetLength()))
{ // LOG_DEBUG (Same as TRACE) output are shown in comment
DWORD code(0);
phttpfile->QueryInfoStatusCode(code);
LOG_DEBUG("HttpPutThread result code: %d", code); // '200'
CString object = phttpfile->GetObject();
LOG_DEBUG("object: %s", object); // 'settings.xml'
CString statustxt;
phttpfile->QueryInfo(HTTP_QUERY_STATUS_TEXT,statustxt);
LOG_DEBUG("status text:%s", statustxt); // 'HTTP/1.0 200 OK'
CString rawheaders;
phttpfile->QueryInfo(HTTP_QUERY_RAW_HEADERS,rawheaders);
LOG_DEBUG("raw headers:%s", rawheaders); // http://localhost:57000/settings.xml
LOG_DEBUG("File url:%s",phttpfile->GetFileURL());
LOG_DEBUG("Verb:%s", phttpfile->GetVerb()); // 'PUT'
} else
{
//This does not happen
LOG_DEBUG("PUT failed in AffHttp::HttpPutThread");
}
} catch(CInternetException* pe)
{
//No exceptions are thrown
LOG_DEBUG("Exception HttpPutThread:%d", pe->m_dwError);
pe->Delete();
}
return 0;
}
提前致谢。