HttpWebRequest 保持session
通過HttpWebRequest獲取網頁內容並保持session,最主要的就是儲存cookie。這裡使用了一個靜態變數m_Cookie用來儲存cookie的內容。第二次請求網頁的時候把cookie傳送過去,這樣就可以保持session。
public partial class RequestPage : System.Web.UI.Page
{
private static CookieContainer m_Cookie = new CookieContainer();
private string m_Url = "http://localhost/HttpRequestTest/SessionPage.aspx";
protected void Page_Load(object sender, EventArgs e)
{
string content = GetPageContent();
//string content = GetPageContent(m_Url);
Label1.Text = content;
}
/// <summary>
/// 獲取頁面內容,儲存CookieHeader
/// </summary>
/// <returns></returns>
private string GetPageContent()
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(m_Url);
request.CookieContainer = m_Cookie;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string cookieheader = request.CookieContainer.GetCookieHeader(new Uri(m_Url));
m_Cookie.SetCookies(new Uri(m_Url), cookieheader);
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
string result = reader.ReadToEnd();
stream.Close();
reader.Close();
response.Close();
return result;
}
/// <summary>
/// 獲取頁面內容,儲存CookieContainer
/// </summary>
/// <param name="url">被請求頁面的url</param>
/// <returns>返回頁面內容</returns>
public string GetPageContent(string url)
{
StringBuilder result = new StringBuilder("");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); ;
HttpWebResponse reponse = null;
try
{
request.CookieContainer = m_Cookie;
reponse = (HttpWebResponse)request.GetResponse();
m_Cookie = request.CookieContainer;
Stream rspStream = reponse.GetResponseStream();
StreamReader sr = new StreamReader(rspStream, System.Text.Encoding.Default);
//獲取資料
Char[] read = new Char[256];
int count = sr.Read(read, 0, 256);
while (count > 0)
{
result.Append(read, 0, count);
count = sr.Read(read, 0, 256);
}
}
catch (Exception e)
{
result.Append(e.Message);
}
finally
{
if (reponse != null)
{
reponse.Close();
}
}
return result.ToString();
}
}
相關文章
- httpWebRequest保持session的方法HTTPWebSession
- 用C#使用HttpWebRequest Post資料時如何保持SessionHTTPWebSession
- C#:使用HttpWebRequest後保持Session,再使用一個HttpWebRequest實現瀏覽、下載等效果C#HTTPWebSession
- wap怎樣做到保持session?Session
- python爬蟲如何用session保持登入?Python爬蟲Session
- HttpWebRequest.GetResponse 方法HTTPWeb
- 如何保持一個伺服器上session不過期伺服器Session
- 爬蟲如何利用session方法保持登陸狀態(selenium)爬蟲Session
- 關於requests的session方法無法保持cookie的問題。SessionCookie
- HttpWebRequest和WebClient的用法HTTPWebclient
- httpWebRequest 檔案下載HTTPWeb
- python+pytest介面自動化(10)-session會話保持PythonSession會話
- PHP中如何保持SESSION以及由此引發的一些思考PHPSession
- Windows Phone網路HttpWebRequest用法WindowsHTTPWeb
- C#通過webRequest保持在同一Session中Post資料C#WebSession
- HttpRequest 和HttpWebRequest的區別(轉)HTTPWeb
- HttpWebRequest的timeout和ReadWriteTimeoutHTTPWeb
- 使用C# + httpWebRequest 解析WMTS服務後設資料C#HTTPWeb
- Vue外賣十一:登入成功資訊顯示、瀏覽器cookie+後端session登入狀態保持Vue瀏覽器Cookie後端Session
- 保持會話連線會話
- 程式碼如何保持最新
- laravel session 與 php session配置LaravelSessionPHP
- session和v$session說明Session
- memcached-session-manager session共享Session
- c#中HttpWebRequest使用Proxy實現指定IP的域名請求C#HTTPWeb
- C#中HttpWebRequest:無法建立 SSL/TLS 安全通道 解決方案C#HTTPWebTLS
- HttpWebRequest請求http1.1的chunked的解析問題記錄HTTPWeb
- C# HttpWebRequest 方式提交資料,引數為普通鍵值對C#HTTPWeb
- SessionSession
- Session分散式共享 = Session + Redis + NginxSession分散式RedisNginx
- session bean中的session如何理解?SessionBean
- HttpWebRequest多執行緒效能問題,請求超時的錯誤HTTPWeb執行緒
- HTTP狀態保持的原理HTTP
- 如何保持最佳 MacBook 溫度?Mac
- 如何讓tableView保持順暢View
- 玩轉跟蹤(to owner session、other session)Session
- session switchSession
- session 共享Session