|
|
| Username / password with WebRequest? |
|
| Author |
Message |
Yaron

|
Posted: Visual C# General, Username / password with WebRequest? |
Top |
Hi, I am looking to have a program which connects to a webpage that requires a username and password, and then downloads a file from this site once it connects. Is it possible to pass username and password credentials using the WebRequest class If so, what methods would I be using to accomplish this Thanks so much, Yaron
Visual C#14
|
| |
|
| |
 |
Vijaye Raji

|
Posted: Visual C# General, Username / password with WebRequest? |
Top |
WebRequest request = WebRequest.Create("http://foo.bar/file.doc"); request.Credentials = new System.Net.NetworkCredential("username", "password");
|
| |
|
| |
 |
rgerbig

|
Posted: Visual C# General, Username / password with WebRequest? |
Top |
|
| |
 |
Yaron

|
Posted: Visual C# General, Username / password with WebRequest? |
Top |
Hi, Thank you for your responses. I have looked at the help and have come up with code that looks like this: WebRequest request = WebRequest.Create("https://login.cwbc.com/login"); request.Method = "POST"; string postData = "usr_name=SOME_NAME&usr_password=SOME_PASSWORD&HiddenURI=https://www.cwbc.com/Partner/Login.asp&AUTHMETHOD=UserPassword"; byte[] byteArray = Encoding.UTF8.GetBytes(postData); request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = byteArray.Length; Stream dataStream = request.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); WebResponse response = request.GetResponse(); dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); string responseFromServer = reader.ReadToEnd(); Console.WriteLine(responseFromServer);
However all I am having returned to me is the login page. Am I constructing the POST parameters correctly (in the code I show above I removed the real user name and password, but with the correct ones I am still only getting the login page returned to me).
|
| |
|
| |
 |
RamiKhalyleh

|
Posted: Visual C# General, Username / password with WebRequest? |
Top |
Yaron wrote: | Hi, Thank you for your responses. I have looked at the help and have come up with code that looks like this: WebRequest request = WebRequest.Create("https://login.cwbc.com/login"); request.Method = "POST"; string postData = "usr_name=SOME_NAME&usr_password=SOME_PASSWORD&HiddenURI=https://www.cwbc.com/Partner/Login.asp&AUTHMETHOD=UserPassword"; byte[] byteArray = Encoding.UTF8.GetBytes(postData); request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = byteArray.Length; Stream dataStream = request.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); WebResponse response = request.GetResponse(); dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); string responseFromServer = reader.ReadToEnd(); Console.WriteLine(responseFromServer);
However all I am having returned to me is the login page. Am I constructing the POST parameters correctly (in the code I show above I removed the real user name and password, but with the correct ones I am still only getting the login page returned to me). |
|
|
| |
|
| |
 |
| |
|