Hi all,
I want to upload a recorded gpx log direct from my PPC with an MFC based application, but I’d never handled http POSTs before.
Now I set up a function and tested successful it with a test php on my server but it fails on OSM because I don’t know how I have to handle the user (email), password.
The server returns StatusCode 200 (no error) but the html page reports
Upload GPS Trace
1 error prohibited this trace from being saved
There were problems with the following fields:
- User can’t be blank
Can somebody help me? Here is the code:
BOOL SendTrack(CString csUser, CString csPassWD, CString csFileName, CString csTags, CString csDescription, BOOL bPublic)
{
CString strFullPath = _T("http://www.openstreetmap.org/trace/create");
//Just dummy data. This will be replaced by content of the csFileName file
CString csTestData =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\
<gpx version=\"1.0\" creator=\"Glopus - http://www.glopus.de\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \
xmlns=\"http://www.topografix.com/GPX/1/0\" \
xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd\"> \
<time>2007-11-06T20:27:50Z</time> \
<bounds minlat=\"48.000\" minlon=\"11.000\" maxlat=\"48.100\" maxlon=\"11.1000\"/> \
<trk> \
<trkseg>\
<trkpt lat=\"48.05\" lon=\"11.05\">\
<time>2004-09-22T04:18:27Z</time>\
<course>108.550003</course>\
<speed>0.027221</speed>\
</trkpt>\
</trkseg>\
</trk>\
</gpx>";
BOOL bSuccess = TRUE;
CInternetSession session(NULL, 0, PRE_CONFIG_INTERNET_ACCESS);
CHttpConnection * pServer = NULL;
CHttpFile * pFile = NULL;
CString strServerName;
CString strObject;
INTERNET_PORT nPort;
DWORD dwServiceType;
CHttpFile *pHTTP = 0;
TCHAR szError[MAX_PATH];
if (!AfxParseURL(strFullPath, dwServiceType, strServerName, strObject, nPort) ||
dwServiceType != INTERNET_SERVICE_HTTP)
return FALSE;
session.EnableStatusCallback(TRUE);
pServer = session.GetHttpConnection(strServerName, nPort, csUser, csPassWD);
try
{
CString csPreFileData, csPostFileData, csHeader, csBoundary = "xyzw";
// I tried to send the Authorization here, but no success
/*
CBase64 Encoder;
CString UserPW = csUser + _T(":") + csPassWD;
UserPW = Encoder.Encode(UserPW, UserPW.GetLength());
csHeader += _T("Authorization: Basic ") + UserPW + _T("\r\n");
*/
csHeader += _T("Content-Type: multipart/form-data; boundary=") + csBoundary + _T("\r\n");
// also this was not accepted /*
csPreFileData += _T("--") + csBoundary;
csPreFileData += _T("\r\n");
csPreFileData += _T("Content-Disposition: form-data; name=\"trace[user]\"");
csPreFileData += _T("\r\n\r\n");
csPreFileData += csUser;
csPreFileData += _T("\r\n");
*/
csPreFileData += _T("--") + csBoundary;
csPreFileData += _T("\r\n");
csPreFileData += _T("Content-Disposition: form-data; name=\"trace[public]\"");
csPreFileData += _T("\r\n\r\n");
csPreFileData += bPublic?_T("1"):_T("0");
csPreFileData += _T("\r\n");
csPreFileData += _T("--") + csBoundary;
csPreFileData += _T("\r\n");
csPreFileData += _T("Content-Disposition: form-data; name=\"trace[description]\"");
csPreFileData += _T("\r\n\r\n");
csPreFileData += csDescription;
csPreFileData += _T("\r\n");
csPreFileData += _T("--") + csBoundary;
csPreFileData += _T("\r\n");
csPreFileData += _T("Content-Disposition: form-data; name=\"trace[tagstring]\"");
csPreFileData += _T("\r\n\r\n");
csPreFileData += csTags;
csPreFileData += _T("\r\n");
csPreFileData += _T("--") + csBoundary;
csPreFileData += _T("\r\n");
csPreFileData += _T("Content-Disposition: form-data; name=\"trace[gpx_file]\"; filename=\"") + csFileName + "\"";
csPreFileData += _T("\r\n");
csPreFileData += _T("Content-Type: text/xml");
csPreFileData += _T("\r\n\r\n");
csPostFileData = _T("\r\n");
csPostFileData += _T("--") + csBoundary + _T("--");
csPostFileData += _T("\r\n");
DWORD dwSize = (DWORD)csPreFileData.GetLength() + (DWORD)csPostFileData.GetLength() + (DWORD)csTestData.GetLength();
pHTTP = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST, strObject, NULL,1,NULL,"HTTP/1.0",INTERNET_FLAG_RELOAD);
pHTTP->AddRequestHeaders(csHeader);
pHTTP->SendRequestEx(dwSize, HSR_SYNC | HSR_INITIATE);
#ifdef _UNICODE
pHTTP->Write(W2A(csPreFileData), csPreFileData.GetLength());
pHTTP->Write(W2A(csTestData), csTestData.GetLength());
pHTTP->Write(W2A(csPostFileData), csPostFileData.GetLength());
#else
pHTTP->Write((LPSTR)(LPCSTR)csPreFileData, csPreFileData.GetLength());
pHTTP->Write((LPSTR)(LPCSTR)csTestData, csTestData.GetLength());
pHTTP->Write((LPSTR)(LPCSTR)csPostFileData, csPostFileData.GetLength());
#endif
pHTTP->EndRequest(HSR_SYNC);
DWORD dwRet;
pHTTP->QueryInfoStatusCode(dwRet);
if (dwRet != 200)
throw;
CString csResponse;
dwSize = (DWORD)pHTTP->GetLength();
while (0 != dwSize)
{
char *szResponse = (LPSTR)malloc(dwSize + 1);
szResponse[dwSize] = '\0';
pHTTP->Read(szResponse, dwSize);
csResponse += CString(szResponse);
free(szResponse);
dwSize = (DWORD)pHTTP->GetLength();
}
AfxMessageBox(csResponse);
}
catch (CException* e)
{
e->GetErrorMessage(szError, MAX_PATH);
e->Delete();
AfxMessageBox(szError);
bSuccess = FALSE;
}
pHTTP->Close();
delete pHTTP;
if ( pServer != NULL )
{
pServer->Close();
delete pServer;
}
session.Close();
return bSuccess;
}
Thanks,
Peter