Im requesting on a web server with Basic Authent and the server is supposed to return a cookie.It does when i use a web brower i can check it in FiddlerProxy... When i debug my code the response.cookie remains null. Here is my code.Thanks for your help
public void ConnexionNT(string password,string user)
{
CookieContainer _cookiecontainer=new CookieContainer();
try
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL_CONNEXION);
request.Method = "GET";
request.Credentials = new NetworkCredential(user, password, domain);
request.CookieContainer = _cookiecontainer;
request.BeginGetResponse(new AsyncCallback(GetResponse), request);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void GetResponse(IAsyncResult MyresponseAsync)
{
HttpWebRequest request = (HttpWebRequest)MyresponseAsync.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(MyresponseAsync);
if (response.Cookies!=null) //REMAINS NULL
{ //SAVE COOKIE}
}
EDIT
If i move the Cookiecontainer decalrtion at the class level the cookie property of httpwebresponse is not null anymore...
EDIT2
Is this code correct to receive cookie from the server ?
public void ConnexionNT(string password,string user)
{
try
{
HttpWebRequest request= (HttpWebRequest)HttpWebRequest.Create(URL_CONNEXION);
request.Method = "GET";
request.Credentials = new NetworkCredential(user, password, domain);
request.CookieContainer = _cookiecontainer;
request.BeginGetResponse(new AsyncCallback(GetResponse),request);
}
catch(HttpRequestException)
{
MessageBox.Show("Un problème de connexion avec le serveur a eu lieu.", "Echec Authentification", MessageBoxButton.OK);
}
catch (Exception )
{
MessageBox.Show("Une erreur a eu lieu","Echec Authentification", MessageBoxButton.OK);
}
}
private void GetResponse(IAsyncResult MyresponseAsync)
{
HttpWebRequest request = (HttpWebRequest)MyresponseAsync.AsyncState;
if (request != null)
{
try
{
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(MyresponseAsync);
//Not null but EMPTY WHY? if (response.Cookies != null)
{
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream isfs = isf.OpenFile("CookiesFile", FileMode.Create, FileAccess.Write))
{
using (StreamWriter sw = new StreamWriter(isfs))
{
foreach (Cookie cookieValue in response.Cookies)
{
sw.WriteLine("Cookie: " + cookieValue.ToString());
}
sw.Close();
}
}
//declenche l'event pour lancer la navigation sur la vue "codepin"
if (event_GetCookie != null && response.Cookies.Count>0)
event_GetCookie.Invoke();
}
}
}