Username / password with WebRequest?  
Author Message
Yaron





PostPosted: 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#10  
 
 
Vijaye Raji





PostPosted: 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





PostPosted: Visual C# General, Username / password with WebRequest? Top

Hi,

thereare two ways to send messages to the web:

1) in a url (get-method)
2) in a stream (post-method)

Look here:

http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpref/html/frlrfsystemnethttpwebrequestclasstopic.asp

The GetResponse method makes a synchronous request to the Internet resource specified in the RequestUri property and returns an HttpWebResponse instance containing the response. You can make an asynchronous request to the Internet resource using the BeginGetResponse and EndGetResponse methods.

When you want to send data to the Internet resource, the GetRequestStream method returns a Stream instance to use to send data. The BeginGetRequestStream and EndGetRequestStream methods provide asynchronous access to the send data stream.



 
 
Yaron





PostPosted: 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





PostPosted: 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).