Witam, zastanawiam się, czy spotkał się już ktoś z takim problemem:
Kiedy używam klasy httpwebrequest program trochę dziwnie sie zachowuje. Po bliższym przyjrzeniu się zauważyłem, że każde kolejne wywołanie ma inną wartość cookie phpsessid. Przez to ciągle gubiona jest sesja. Ma ktoś pomysł jak można rozwiązać problem?
Kod:
public class Polaczenie
{
HttpWebRequest strona = null;
public CookieContainer cookies = new CookieContainer();
private string connParam = "Connection: keep-alive";
private string uagentParam = "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; pl; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4";
string host;
public Polaczenie(string host)
{
this.host = host;
}
public StreamReader SendPost(String addres, String PostValues){
strona = (HttpWebRequest)WebRequest.Create(addres);
strona.KeepAlive=true;
strona.Method = WebRequestMethods.Http.Post;
strona.ContentType = "application/x-www-form-urlencoded";
strona.ContentLength = PostValues.Length+connParam.Length+uagentParam.Length+6;
strona.CookieContainer= cookies;
StreamWriter requestStream = new StreamWriter(strona.GetRequestStream());
try{
requestStream.WriteLine(PostValues);
requestStream.WriteLine(uagentParam);
requestStream.WriteLine(connParam);
requestStream.Flush();
}catch(Exception ex){
Console.WriteLine(ex);
}
try{
HttpWebResponse odpowiedz = (HttpWebResponse)strona.GetResponse();
StreamReader responseStream = new StreamReader(odpowiedz.GetResponseStream());
return responseStream;
}catch(Exception ex){
Console.WriteLine(ex);
return null;
}
}
public StreamReader SendGet(string addres){
strona = (HttpWebRequest)WebRequest.Create(addres);
strona.KeepAlive=true;
strona.Method = WebRequestMethods.Http.Get;
//strona.ContentType = "application/x-www-form-urlencoded";
strona.CookieContainer = cookies;
try{
HttpWebResponse odpowiedz = (HttpWebResponse)strona.GetResponse();
StreamReader responseStream = new StreamReader(odpowiedz.GetResponseStream());
return responseStream;
}catch(Exception ex){
Console.WriteLine(ex);
return null;
}
}
}