2011-12-06 10 views
0

認証が必要なサイトからデータを取得するプログラムを作っています。私はコンソールでプログラムを作っているので、コントロールを使うことはできません。Webリクエストによる認証を覚えている

ログインフォームにユーザーデータを送信し、ウェルカム画面の情報を取得できました。しかし、別のリクエストを送信すると、私はログインしたことを忘れてしまいました。 私は何とかクッキーを有効にしなければならなかったが、私はまだそれを働かせていません。

これは私のコードです:

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 
+0

レスポンスヘッダーのクッキーを読み取って保存し、その後のリクエストで送信する必要があります。 –

+0

これに完全な答えを出すには、実際には十分ではありません。あなたが本当に必要とするのは、後続のリクエストで応答に返されるCookieコレクションを含めることです。しかし、私は、このコードに基づいて、そのクッキーの形式または内容がどのようなものであるべきかを知りません。 –

+0

質問にhttpヘッダーを追加しました。 – Jerodev

答えて

3

あなたはクッキーコンテナを追加する必要があります。

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. 
+0

これは完璧に機能しました、ありがとうございました。 – Jerodev

関連する問題