How to query version info via C#

Hello,

I am trying to query the version info off of TeamCity.  I can do it simple enough via Internet Explorer:

http://<server>/app/rest/buildTypes/id:bt<id>/builds/status:SUCCESS/number

It simply returns:

1.0.430.9431

But what I'd like to do is this same action via C#.  Here's what I have so far:

const string PATH = "http://...";

using (var wc = new WebClient())
{
     string result = wc.DownloadString(PATH)
}

This won't work because the result I get back is the HTML requesting that I log in.

Ok, I think it wants me to send credentials.  So before the call to download the string, I added:

wc.Credentials = new NetworkCredential("<userid>", "<password>");

I use my Team City userid/password, but I'm still getting the login html.

What I think IE does is that it passes it's "RememberMe" cookie, which substitutes as the necessary credentials.

Can someone provide me the necessary code to make the authentication happy?

Thanks in advance,
--Eric

1 comment
Comment actions Permalink

I figured out an answer:

I used fiddler to track down what cookies were being used, and found it was using a cookie named "RememberMe"

This code works: (Substitute accordingly for <server>, <id>, and <cookie>)
(Also, take about the brackets around [http], this editor turns that text into a hyperlink)


const string PATH = "[http]://<server>/app/rest/buildTypes/id:bt<id>/builds/status:SUCCESS/number";
const string COOKIE = "RememberMe=<cookie>"; using (var wc = new WebClient())
{

wc.Headers.Add(HttpRequestHeader.Cookie, COOKIE);
string result = wc.DownloadString(PATH);

}

0

Please sign in to leave a comment.