如何使用JSESSIONID c#获取请求?

时间:2018-05-07 20:12:14

标签: c# jsessionid

我遇到了C#HTTP GET请求的问题。 我向服务器发送了两个请求: 第一个请求返回JSESSIONID的值,它的工作是:

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> " + resp.Headers);
                    }
                }
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
    return id;
}

第二个请求应该将服务器响应返回给我的搜索,但它会返回错误" 302暂时移动"或"注意会话已过期"。

private void RequestFromServer(string url)
{
    string session = GetSessionId().Split(';')[0];
    Console.WriteLine("Session> {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&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());
    }
}

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

这里你可能需要的是CookieContainer。而不是

webRequest.Headers.Add("Cookie", session);
你应该做点什么

webRequest.CookieContainer = new CookieContainer();
Cookie cookie = new new Cookie("JSESSIONID", yourSessionIdHere);
// adjust domain accordingly
cookie.Domain = "www.xxxxxxxxxxxxxxx.it";
httpRequest.CookieContainer.Add(cookie);

代码未经测试。

MSDN网站还声明:

  

出于安全原因,默认情况下会禁用Cookie。如果您想使用cookie,请使用CookieContainer属性启用cookie。

这意味着为了使用cookie,您必须明确设置CookieContainer。