VC++ 向 WCF服务端 上传 json数据
灿烂李 2012-10-26 05:45:19 用的是VC++
我传送的内容 本身 经验证是 json格式。
是否是通讯部分 出了问题 有明白人帮看下吗 多谢了。
url = "http://www.joyparty.net/ToonyooWebservice/CustomerTrafficService/CTEntry/InsertCustomerInOutRecord";
urlData = "ApiKey=test&Sign=985E40591E343020FF8086CDB9&json=[{\"id\": 1,\"shopNo\": \"BJWD\",\"channel\": 0,\"name\": \"channelTest\",\"in_num\": 0,\"out_num\": 5,\"record_time\": \"2012-10-21 10:58:14\"}]";
webAccess.Post(url, urlData, result);
通讯部分:
// http connection
CHttpConnection *pHttpConnection;
// http file pointer
CHttpFile *pHttpFile;
// server url and object.
CString szServerUrl, szObject;
//CString strHeaders = _T("Content-Type: application/x-www-form-urlencoded");
CString strHeaders = _T("Content-Type: application/json");
// extract server and objects
szServerUrl = url;
ExtractObject(szServerUrl,szObject);
try
{
pHttpConnection=GetHttpConnection(szServerUrl);
if( NULL == pHttpConnection)
{
// no exception raised but there is an error.
return WEB_ACCESS_UNEXPECTED_ERROR;
}
}
catch (CInternetException *pException)
{
char buffer[1023];
pException->GetErrorMessage(buffer, 1023);
resultString = buffer;
return pException->m_dwError;
}
// open request
try {
pHttpFile = pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, szObject);
if(NULL == pHttpFile)
{
return WEB_ACCESS_UNEXPECTED_ERROR;
}
}
catch(CInternetException *pException)
{
char buffer[1023];
pException->GetErrorMessage(buffer, 1023);
resultString = buffer;
return pException->m_dwError;
}
// send the request
try {
BOOL ret = pHttpFile->SendRequest(strHeaders, (LPVOID)(LPCSTR)szFormData, szFormData.GetLength());
if( FALSE == ret )
{
return WEB_ACCESS_UNEXPECTED_ERROR;
}
}
catch(CInternetException *pException)
{
char buffer[1023];
pException->GetErrorMessage(buffer, 1023);
resultString = buffer;
return pException->m_dwError;
}
// query status code
DWORD retCode;
BOOL ret = pHttpFile->QueryInfoStatusCode(retCode);
if( FALSE == ret )
{
return WEB_ACCESS_QUERY_INFO_ERROR;
}
else if( HTTP_STATUS_OK != retCode )
{
char buf[2];
int bytesRead;
resultString = "";
while( (bytesRead = pHttpFile->Read(buf, 1)) > 0 )
{
resultString += buf[0];
}
return retCode;
}
返回内容:
The server encountered an error processing the request. Please see the <a rel="help-page" href="http://www.joyparty.net/ToonyooWebservice/CustomerTrafficService/CTEntry/help">service help page</a> for constructing valid requests to the service.
The exception message is 'The incoming message has an unexpected message format 'Raw'.
The expected message formats for the operation are 'Xml', 'Json'. This can be because a WebContentTypeMapper has not been configured on the binding.
See the documentation of WebContentTypeMapper for more details.'. See server logs for more details. The exception stack trace is: </p>
<p> at System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message message, Object[] parameters)
at System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</p>