title: How to get request with JSESSIONID c#?
body: <p>I've a problem with a C# HTTP GET request. 
  I send two requests to server:
  The first request returns the value of JSESSIONID and it's work:</p>
  
  <pre><code>private string GetSessionId(){
  {
      string id = null;
      try
      {
          var webRequest = WebRequest.Create("http://www.xxxxxxxxxxxxxxx.it/home") as HttpWebRequest;
          var response = "";
  
          webRequest.Date = DateTime.UtcNow;
          webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
          webRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
          webRequest.Headers.Add("Accept-Language", "it-IT,it;q=0.9,en-US;q=0.8,en;q=0.7,de-DE;q=0.6,de;q=0.5");
          webRequest.Connection = "keep-alive";
          webRequest.Headers.Add("Cache-Control", "max-age=0");
          webRequest.Host = "www.xxxxxxxxxxxxxxxx.it";
          webRequest.Headers.Add("Upgrade-Insecure-Requests", "1");
          webRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36";
  
          if (webRequest != null)
          {
              webRequest.Method = "GET";
              using (WebResponse resp = webRequest.GetResponse())
              {
                  using (Stream s = resp.GetResponseStream())
                  {
                      using (StreamReader sr = new StreamReader(s))
                      {
                          id = resp.Headers["Set-Cookie"];
                          response = sr.ReadToEnd();
                          Console.WriteLine("Response1&gt; " + resp.Headers);
                      }
                  }
              }
          }
      }
      catch (Exception ex)
      {
          Console.WriteLine(ex.ToString());
      }
      return id;
  }
  </code></pre>
  
  <p>The second request should return the server response to my search but it return an error "302 Moved Temporarily" or "Attention the session has expired". </p>
  
  <pre><code>private void RequestFromServer(string url)
  {
      string session = GetSessionId().Split(';')[0];
      Console.WriteLine("Session&gt; {0}",session);
  
      try
      {
          HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest;
          var response = "";
  
          if (webRequest != null)
          {
              Uri target = new Uri(url);
              webRequest.Method = "GET";
              webRequest.Timeout = 20000;
              webRequest.Date = DateTime.UtcNow;
              webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
              webRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
              webRequest.Headers.Add("Accept-Language", "it-IT,it;q=0.9,en-US;q=0.8,en;q=0.7,de-DE;q=0.6,de;q=0.5");
              webRequest.Headers.Add("Cache-Control", "max-age=0");
              webRequest.Connection = "keep-alive";
              webRequest.ContentType = "application/x-www-form-urlencoded";
              webRequest.Headers.Add("Cookie", session);
              webRequest.Host = "www.xxxxxxxxxxxxxx.it";
              webRequest.Headers.Add("Origin", "http://www.xxxxxxxxxxxx.it");
              webRequest.Referer = "http://www.xxxxxxxxxxxxxxxx.it/ricerca/atto/contratti/originario?reset=true&amp;normativi=false";
              webRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36";
              webRequest.Headers.Add("Upgrade-Insecure-Requests", "1");
  
              var res = webRequest.GetResponse();
              using (Stream s = res.GetResponseStream())
              {
                  using (StreamReader sr = new StreamReader(s))
                  {
                      response = sr.ReadToEnd();
                      Console.Write(res.Headers);
                      Console.WriteLine(response);
                  }
              }
          }
      }
      catch (Exception ex)
      {
          Console.WriteLine(ex.ToString());
      }
  }
  </code></pre>
  
  <p>Can someone help me with this problem?</p>