I've been working on a twitter interface for StationRipper.  StationRipper is written in C++/MFC, so I don't have some of the handy new XMLDOM stuff available.  So here's how to interface to Twitter in C++/MFC:

To call it, you would do something like:

CString sErr = "";
SetTwitterStatus("This message was sent from StationRipper.", "YourUserID", "yourpassword", sErr);


//------------------------------------------------------------------------
//
// Method: SetTwitterStatus
//
// Description: Internet files returned by OpenURL do not correctly return
// the size of the returned file. To force a read of the
// entire file, use this function. It will read nBlocks from
// the file, until it has read the entire file. The returning
// size will be the entire file.
//
//More info at http://www.stationripper.com/twitter
//
// Parms: sStatus- The text of the status message.
//sUserName- The Twitter username to login as.
//sPassword- The Twitter password to login as.
//
//
// Return: bool- TRUE = The message was posted.
//
// Exceptions: N/A
//
// C H A N G E L O G
// =====================================================================
// Change ID Date Programmer Description
// ============ ========= =========== ================================
// 3-18-07 Ratajik Initial Development
//_________________________________________________________________________
bool CUtil :: SetTwitterStatus(const CString& sStatus, const CString& sUserName, const CString& sPassword, CString& sErr)
{
#define CHUNK_SIZE 2048
bool bRet = false;
try {


//
// Instantiate CInternetSession
//
CInternetSession httpSession(_T("Test of MFC Twitter Connection"),
1,
INTERNET_OPEN_TYPE_PRECONFIG,
NULL,
NULL,
INTERNET_FLAG_DONT_CACHE);

//
// Get CHttpConnection (Server URL and Port required)
//
CHttpConnection* pHttpConnection =
httpSession.GetHttpConnection(_T("twitter.com"),
INTERNET_FLAG_NO_AUTO_REDIRECT,
80, sUserName, sPassword);

//
// Open HTTP Request (pass method type [get/post/..] and URL
// path (except server name))
//
CHttpFile* pHttpFile =
pHttpConnection->OpenRequest
(_T("POST"),
_T("statuses/update.xml"),
NULL, 1, NULL, NULL,
INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_NO_COOKIES |
INTERNET_FLAG_RELOAD);

CString strHeaders = _T ("Content-Type:application/x-www-form-urlencoded");

pHttpFile->AddRequestHeaders(strHeaders);

CString sRequest = "status=" + sStatus;

//
// Send the request
//
pHttpFile->SendRequest(NULL, 0, (LPVOID)(LPCTSTR)sRequest,
                      sRequest.GetLength());

//
// Check the return HTTP Status Code
//
DWORD dwStatusCode = HTTP_STATUS_OK;

pHttpFile->QueryInfoStatusCode(dwStatusCode);

//
// Get whatever was returned.
//
if(dwStatusCode == HTTP_STATUS_OK)
{
bRet = true;

CString strResponse;
TCHAR szBuf[CHUNK_SIZE] = {0};
UINT nBytesRead;

// 7. Read the response text
do {
nBytesRead = pHttpFile->Read((void*) szBuf, CHUNK_SIZE);
strResponse += szBuf;
if(nBytesRead < CHUNK_SIZE)
break;
}while(nBytesRead == CHUNK_SIZE);

//
// In StationRipper, this will be called from a thread, so we
// don't want this. If you are debugging, uncomment this to see
// what was returned.
//
// AfxMessageBox(strResponse);
}
else {
sErr = "";
sErr.Format("Non-ok Status: %d" + dwStatusCode);
}

//
// Close the stream/connection
//
if(pHttpFile)
{
pHttpFile->Close();
delete pHttpFile;
pHttpFile = NULL;
}

if(pHttpConnection)
{
pHttpConnection->Close();
delete pHttpConnection;
pHttpConnection = NULL;
}

}
catch(CInternetException* exp)
{
TCHAR lpszErrorMsg[MAX_PATH+2];
exp->GetErrorMessage(lpszErrorMsg, MAX_PATH);
sErr = "Exception: " + *lpszErrorMsg;
}

return(bRet);
}  

 


Who Should Microsoft Hire?
iTunes Radio Ripper Record iTunes Record Radio Streams Record Radio Record iTunesRadio