I'm writing a program in visual studio 2010 using C# .Net.
The program will access multiple parts of the website (different urls), and I'm using HttpWebRequest
and NetworkCredential
to set username and password.
Right now, every time I send in a request, I have to set the credential. Is there a way to set the credential only once through out the whole process?
public void setCredential(string url)
{
//set username and password
//and get response
}
public void Main()
{
string[] URLs= { url1, url2, url3 };
foreach (string url in URLs)
{
setCredential(url);
}
}
Like I mentioned, by doing it this way, every time I go to a new url I will have to set the credential, which makes it slow sometimes.
I tried to set the credential outside the foreach loop but then I was not able to get response for url2 and url3.
I'm wondering if there's a better way to do it?
-- Edit 10:26am --
Sorry if I'm not clear.
What I meant is that every time I try to access a url, I had to re-send the username and password over to the server.
So for the sample code, I will have to send the username and password 3 times for 3 different url.
I would like to know if I can send the username and password only once and access all 3 different urls.
ps. All 3 urls are accessible with the same username and password.
Passing NetworkCredential to HttpWebRequest in C# from ASP.Net Page
This link is showing NetworkCache, this may be a solution for you.