记住通过 Web 请求进行身份验证

发布于 2024-12-20 00:32:28 字数 2837 浏览 1 评论 0原文

我正在制作一个程序,从需要身份验证的站点获取数据。我正在控制台中编写程序,因此无法使用控件。

我能够将用户数据发送到登录表单并获取欢迎屏幕的信息。但是当我发送另一个请求时,该网站忘记了我曾经登录过。 我读到我必须以某种方式启用cookie,但我还没有解决。

这是我的代码:

public string GetResponse()
{
    // Build a string containing all the parameters
    string Parameters = string.Empty;
    foreach (string p in theQueryData)
    {
        Parameters += string.Format("&{0}", p);
    }

    if (Parameters.Length > 0)
        Parameters = Parameters.Substring(1);

    // Create a request using a URL that can receive a post. 
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.url);

    // Set the Method property of the request to POST.
    request.Method = this.method;
    // Create POST data and convert it to a byte array.
    string postData = Parameters;
    byte[] byteArray = Encoding.UTF8.GetBytes(postData);
    // Set the ContentType property of the WebRequest.
    request.ContentType = "application/x-www-form-urlencoded";
    // Set the ContentLength property of the WebRequest.
    request.ContentLength = byteArray.Length;
    // Get the request stream.
    Stream dataStream = request.GetRequestStream();
    // Write the data to the request stream.
    dataStream.Write(byteArray, 0, byteArray.Length);
    // Close the Stream object.
    dataStream.Close();
    // Get the response.
    WebResponse response = request.GetResponse();
    // Get the stream containing content returned by the server.
    dataStream = response.GetResponseStream();
    // Open the stream using a StreamReader for easy access.
    StreamReader reader = new StreamReader(dataStream);
    // Read the content.
    string responseFromServer = reader.ReadToEnd();
    // Clean up the streams.
    reader.Close();
    dataStream.Close();
    response.Close();

    return responseFromServer;
}

这些是请求返回的标头:

Connection: keep-alive
Vary: Accept-Encoding
Content-Length: 7628
Cache-Control: private
Content-Type: text/html
Date: Tue, 06 Dec 2011 20:58:21 GMT
Set-Cookie: plaza%5Fmasteraccount=; expires=Tue, 06-Dec-2011 23:00:00 GMT; domain=; path=/,plaza%5Fvacmode=R; expires=Tue, 06-Dec-2011 23:00:00 GMT; domain=; pa
th=/,plaza%5Fname=%16%05%0C%0E%15%09%18%21QRQ; expires=Tue, 06-Dec-2011 23:00:00 GMT; domain=; path=/,plaza%5Flocked=R; expires=Tue, 06-Dec-2011 23:00:00 GMT; d
omain=; path=/,plaza%5Flogin=%16%05%0C%0E%15%09%18%21QRQ; expires=Tue, 06-Dec-2011 23:00:00 GMT; domain=; path=/,plaza%5Fid=S%5ENWNSQPP%40%15%08%0C%0E%15%09%18%
21QRQ%15%08%0C%0E%15%09%18%21QRQRQQ; expires=Tue, 06-Dec-2011 23:00:00 GMT; domain=; path=/,plaza%5Fmid=1323205117; expires=Sun, 25-Nov-2012 23:00:00 GMT; path=
/,plaza%5Fstatus=Q; expires=Tue, 06-Dec-2011 23:00:00 GMT; domain=; path=/,ASPSESSIONIDSQCRSDDR=NKHJIBADOGALNGKDBALDJBIA; path=/
Server: nginx
X-Powered-By: ASP.NET

I am making a program that get's it's data from a site that needs authentication. I am making the program in a console, so I cannot use controls.

I am able to send the user data to the login form and get the information of the welcome screen. But when I send another request, the site forgot I ever logged in.
I read that I had to enable cookies somehow, but I haven't worked it out yet.

This is my code:

public string GetResponse()
{
    // Build a string containing all the parameters
    string Parameters = string.Empty;
    foreach (string p in theQueryData)
    {
        Parameters += string.Format("&{0}", p);
    }

    if (Parameters.Length > 0)
        Parameters = Parameters.Substring(1);

    // Create a request using a URL that can receive a post. 
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.url);

    // Set the Method property of the request to POST.
    request.Method = this.method;
    // Create POST data and convert it to a byte array.
    string postData = Parameters;
    byte[] byteArray = Encoding.UTF8.GetBytes(postData);
    // Set the ContentType property of the WebRequest.
    request.ContentType = "application/x-www-form-urlencoded";
    // Set the ContentLength property of the WebRequest.
    request.ContentLength = byteArray.Length;
    // Get the request stream.
    Stream dataStream = request.GetRequestStream();
    // Write the data to the request stream.
    dataStream.Write(byteArray, 0, byteArray.Length);
    // Close the Stream object.
    dataStream.Close();
    // Get the response.
    WebResponse response = request.GetResponse();
    // Get the stream containing content returned by the server.
    dataStream = response.GetResponseStream();
    // Open the stream using a StreamReader for easy access.
    StreamReader reader = new StreamReader(dataStream);
    // Read the content.
    string responseFromServer = reader.ReadToEnd();
    // Clean up the streams.
    reader.Close();
    dataStream.Close();
    response.Close();

    return responseFromServer;
}

These are the headers the request returns:

Connection: keep-alive
Vary: Accept-Encoding
Content-Length: 7628
Cache-Control: private
Content-Type: text/html
Date: Tue, 06 Dec 2011 20:58:21 GMT
Set-Cookie: plaza%5Fmasteraccount=; expires=Tue, 06-Dec-2011 23:00:00 GMT; domain=; path=/,plaza%5Fvacmode=R; expires=Tue, 06-Dec-2011 23:00:00 GMT; domain=; pa
th=/,plaza%5Fname=%16%05%0C%0E%15%09%18%21QRQ; expires=Tue, 06-Dec-2011 23:00:00 GMT; domain=; path=/,plaza%5Flocked=R; expires=Tue, 06-Dec-2011 23:00:00 GMT; d
omain=; path=/,plaza%5Flogin=%16%05%0C%0E%15%09%18%21QRQ; expires=Tue, 06-Dec-2011 23:00:00 GMT; domain=; path=/,plaza%5Fid=S%5ENWNSQPP%40%15%08%0C%0E%15%09%18%
21QRQ%15%08%0C%0E%15%09%18%21QRQRQQ; expires=Tue, 06-Dec-2011 23:00:00 GMT; domain=; path=/,plaza%5Fmid=1323205117; expires=Sun, 25-Nov-2012 23:00:00 GMT; path=
/,plaza%5Fstatus=Q; expires=Tue, 06-Dec-2011 23:00:00 GMT; domain=; path=/,ASPSESSIONIDSQCRSDDR=NKHJIBADOGALNGKDBALDJBIA; path=/
Server: nginx
X-Powered-By: ASP.NET

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里兽 2024-12-27 00:32:28

您需要添加一个 cookie 容器。

CookieContainer cookieContainer = new CookieContainer();
// Create a request using a URL that can receive a post. 
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.url);
request.CookieContainer = cookieContainer;

//DO your request that sets cookies from the server.
.........

//Place another request with the cookies
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(someNewUrl);
request.CookieContainer = cookieContainer;
//this should have cookies from the previous request, which should keep you logged in.

You need to add a cookie container.

CookieContainer cookieContainer = new CookieContainer();
// Create a request using a URL that can receive a post. 
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.url);
request.CookieContainer = cookieContainer;

//DO your request that sets cookies from the server.
.........

//Place another request with the cookies
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(someNewUrl);
request.CookieContainer = cookieContainer;
//this should have cookies from the previous request, which should keep you logged in.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文