tags:

views:

243

answers:

1

I am new to plumtree and I am trying to get to know the Plumtree object model. I am trying download a file from the plumtree knowledge directory programmatically.

This is the code I am using:

IRemoteSession session = RemoteSessionFactory.GetExplicitLoginContext(new Uri(url), userName, password);

IDocumentFolderManager DocFolderManager = session.GetDocumentFolderManager();

IDocumentManager DocManager = session.GetDocumentManager();

IObjectQuery FolderQuery = DocFolderManager.GetSubFolders(folderID);

IDocumentQuery DocQuery = DocManager.CreateQuery(FolderQuery.GetRow(i).GetID());

IObjectQuery DocumentQuery = DocQuery.Execute();

IObjectQueryRow document = DocumentQuery.GetRow(1);

IDocumentProperties _docProperties = DocManager.QueryDocumentProperties(document.GetID());

string docUrl = _docProperties.GetStringValue(5);

WebClient webclient = new WebClient();

webclient.Credentials = new System.Net.NetworkCredential(userName, password, "");

webclient.Credentials = CredentialCache.DefaultCredentials;

webclient.DownloadFile(docUrl, "c:\1");

But there seems to be some problem with my session creation code, because instead of downloading the actual file, this code downloads the Plumtree login page to my file system.

Can somebody please tell me what I am doing wrrong here? There must be something that I am missing

+1  A: 

The problem is that your webclient instance does not know how to authenticate against the plumtree server. new NetowrkCredential() or CredentialCache.DefaultCredentials works in simple cases, but clearly not in this one.

Plumtree either uses a cookie or (more likely) a particular login token which is obtained via the call to GetExplicitLoginContext() and then sent along with all subsequent requests in that session.

You can potentially fake this "sent along with all subsequent requests" using WebClient, but you'd need to know more technical details about how Plumtree servers authenticate clients. You can figure out some of this info using Fiddler, but an easier approach may be to find an API that plumtree offers to download files which have been previously uploaded into Plumtree.

Another (hacky) way to approach this is to fake a browser client. Make your request above, then parse the HTML, pull out the action URL of the login form, and then fake up a HttpWebRequest call which looks like a real client filling in the login form. You'll need to use a CookieContainer to make sure cookies are sent, make sure headers are right, etc. See http://channel9.msdn.com/forums/TechOff/162017-Using-WebClient-to-enter-Form-based-Auth-system-How/ for more info.

Justin Grant