Login to XDrive using the .NET Framework

Last week I looked at what it would take to upload a file to XDrive, but in my list of API's involved in uploading a file I actually forgot the most important one: Login. Logging on to XDrive creates a session and the Login API provides you with a crucial bit of information: the jsessionid. You need to pass a reference to the session everytime you make an API call, so that makes it essential to make any application run.

So let's create some C# code to login to XDrive.

Below is a method uses the .NET Framework classes WebRequest and WebResponse to send an HTTP POST message to the XDrive server and retrieve the results.

private string Login()
{
    // url for xdrive login
    string url = "https://plus.xdrive.com/json/v1.2/member.login";

    // the json object that we need to send to the login resource
    string json = "data={\"user\":{\"type\":\"MemberObject\",\"username\":\"mark\",\"password\":\"secret\"}}";
    // convert JSON to bytes
    byte[] bytes = Encoding.ASCII.GetBytes(json);

    // create a request using System.Net.WebRequest
    WebRequest request = WebRequest.Create(url);

    // use form-url encoding
    request.ContentType = "application/x-www-form-urlencoded";

    // the API will only take POST requests
    request.Method = "POST";

    // length of the request we're sending
    request.ContentLength = bytes.Length;
   
    Stream os = null;
    try
    {
        // get a stream to write to and write the bytes to the stream
        os = request.GetRequestStream();
        os.Write(bytes, 0, bytes.Length);
    }
    catch (WebException exception)
    {
        MessageBox.Show(exception.ToString());
        return null;
    }
    finally
    {
        // close the stream in the finally
        if (os != null)
        {
            os.Close();
        }
    }

    string result = null;

    try
    { 
        // use the request object to get to the response
        WebResponse response = request.GetResponse();

        if (response != null)
        {
            StreamReader sr = new StreamReader(response.GetResponseStream());
            result = sr.ReadToEnd();
            MessageBox.Show(result);
        }
    }
    catch (WebException exception)
    {
        MessageBox.Show(exception.ToString());
        return null;
    }
    return ExtractSessionID(result);
}

On the last line you see a method called 'ExtractSessionID'. The code for this method is below. .NET 3.5 offers functionality for parsing JSON objects into .NET objects, but because I wanted this method to also work on .NET 2.0 I decided to implement a string parse routine which extracts the jsessionid from the response.


private string ExtractSessionID(string jsonUserdata)
{

    //{"results":{"user":{"lastname":"Blomsma","rootFolderId":"xdr:XFS-1665063167",
    //"type":"MemberObject","firstname":"Mark","username":"markdeveloper@aim.com","email":
    //"markdeveloper@aim.com","userSeq":3007402}},"recoveryToken":"3007402|bWFya2RldmVsb
    //3BlckBhaW0uY29tfGZKUS92ZTMyb08wWE18MTE5MzI0Mjc5MTI1NXxNQ3dDRkJm
    //MnUvSXN4bG9hZEtIbEtmQW41Tjg3YzRodEFoUmFtTjFPUmdReU04SmRPd1hQUHZlaWpPOUVodz09",
    //"jsessionid":"EB396F97E81FC1005098AAC01E170218.xdr-mtc19.websys.aol.com_6002"}

    if (jsonUserdata != null && jsonUserdata != String.Empty)
    {
        string result = jsonUserdata.Substring(0, jsonUserdata.LastIndexOf("\"")); // get string until last "
        result = result.Substring(result.LastIndexOf("\"") + 1); // get string from last "
        return result;
    }
    // return empty 
    return String.Empty;
}

There are still a couple of MessageBox.Show statements in this sample. Try and run it from a Windows application and this will give you an idea of what kind of data goes across the wire.

Happy coding!

- Mark Blomsma

ahh ok

ok I'll see what I can to do, anyway thanks for post, I like your blog, thanks again Mark

some live variant?

any working variant to see?

some live variant

Hi,

Not really, just these sample to give you copy and paste code to start and modify in your own app.

My XDrive sync project has been put on hold due to other priorities, sorry.

- Mark Blomsma