1) Create a new windows application.
2) Put the following code in the form load
private void Form1_Load(object sender, EventArgs e)
{
string loginUrlString = “https://servername/Services/Integration?command=login”;
String sessionID = ManageSession.Login(loginUrlString, @”orgname-devusername”, “password”);
}
3) Define the login method within ManageSession class in the following manner
public static string SessionID = “”;
public static String Login(String loginUrlString, String userName, String password)
{
try
{
// create a http request and set the headers for authentication
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(loginUrlString);
HttpWebResponse myResponse;
myRequest.Method = “POST”;
// passing username and password in the http header
// username format if it includes slash should be the forward slash /
myRequest.Headers[“UserName”] = userName;
myRequest.Headers[“Password”] = password;
myResponse = (HttpWebResponse)myRequest.GetResponse();
Stream sr = myResponse.GetResponseStream();
// retrieve session id
char[] sep = { ‘;’ };
String[] headers = myResponse.Headers[“Set-Cookie”].Split(sep);
for (int i=0; i <= headers.Length-1; i++)
{
if (headers[i].StartsWith(“JSESSIONID”))
{
sep[0] = ‘=’;
SessionID = headers[i].Split(sep)[1];
break;
}
}
sr.Close();
myResponse.Close();
}
catch (WebException webException)
{
}
catch (Exception ex)
{
}
// send back the session id that should be passed to subsequent calls
// to webservices
return SessionID;
}
That’s it..
