Project

General

Profile

RE: login in rm from application ยป login.txt

mohammed geheim, 2011-01-13 14:12

 
1
public String GetWebContent(String username, String password)
2
		{
3
			String _retVal = "err001";
4
			String _urlLogin = "";
5
			String _urlSignin = "";
6
			String _respStr = "";
7
			String _postData = "";
8

    
9
			Uri _uriLogin = null;
10
			Uri _uriSignin = null;
11

    
12
            _urlLogin = "http://rm.test.nl/projects/tetrahis/issues"; // is the login Page
13
            _urlSignin = "http://rm.test.nl/login?back_url=http%3A%2F%2Frm.test.nl%2Fprojects%2Ftetrahis%2Fissues"; //is the link used inside the action of the form tag (Note: I suppose in this example that the form name inside the _urlLogin page is "loginForm")
14

    
15

    
16
			_uriLogin = new Uri(_urlLogin);
17
			_uriSignin = new Uri(_urlSignin);
18

    
19
			try
20
			{
21
				HttpWebRequest _wReq;
22
				HttpWebResponse _wResp;
23
				System.IO.StreamReader _sr;
24
				System.Text.ASCIIEncoding _enc = new System.Text.ASCIIEncoding();
25

    
26
				CookieContainer _cookies;
27

    
28
				_wReq = (HttpWebRequest) WebRequest.Create(_uriLogin);
29
				_wReq.CookieContainer = new CookieContainer();
30
				_wReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
31
				
32
				_wResp = (HttpWebResponse) _wReq.GetResponse();
33
				_sr = new System.IO.StreamReader(_wResp.GetResponseStream());
34
				_respStr = _sr.ReadToEnd();
35
				_sr.Close();
36
				
37
				_cookies=_wReq.CookieContainer;
38

    
39
				_wResp.Close();
40
                Hashtable _otherData;
41
                // in an initialization routine
42
                _otherData = new Hashtable();
43
                _otherData.Add("username", new Hashtable());
44
                _otherData.Add("password", new Hashtable()); 
45

    
46

    
47
				//Here insert all the parameter needed for the login... eg: in my example I suppose that the loginForm has two input field named "username" and "password"
48
				((Hashtable)_otherData["username"])["value"] = username;
49
				((Hashtable)_otherData["password"])["value"] = password;
50
				//you could need to insert other information
51

    
52
				_postData = "";
53
				foreach(string name in _otherData.Keys)
54
				{
55
					_postData += "&" + name + "=" + ((Hashtable)_otherData[name])["value"];
56
				}
57
				_postData = _postData.Substring(1);
58

    
59
				byte[] _data = _enc.GetBytes(_postData);
60

    
61
				_wReq = (HttpWebRequest) WebRequest.Create(_uriSignin);
62
				_wReq.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
63
				_wReq.Referer = _urlLogin;
64
				_wReq.KeepAlive = true;
65
				_wReq.Method = "POST";
66
				_wReq.ContentType = "application/x-www-form-urlencoded";
67
				_wReq.ContentLength = _data.Length;
68
				_wReq.CookieContainer = _cookies;
69
				_wReq.AllowAutoRedirect = false;
70
				_wReq.UserAgent = "Mozilla/5.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
71

    
72
			
73
				System.IO.Stream _outStream = _wReq.GetRequestStream();
74
				_outStream.Write(_data,0,_data.Length);
75
				
76
				_outStream.Close();
77
				
78
				_wResp = (HttpWebResponse) _wReq.GetResponse();
79
				_sr = new System.IO.StreamReader(_wResp.GetResponseStream());
80
				_respStr = _sr.ReadToEnd();
81
				_sr.Close();
82

    
83
				_wResp.Close();
84
								
85
				// _respStr contains the page content... use this for your needed.
86
				_retVal=_respStr;
87
			}
88

    
89
            catch(Exception ex)
90
			{
91
				string err = ex.ToString();
92
				throw ex;
93
				//Response.Write(e.ToString());
94
				return null;
95
			}
96

    
97
            return _retVal;
98
		}
    (1-1/1)