zoukankan      html  css  js  c++  java
  • Windows Mobile CookieContainer

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Net;

    namespace net.frejos.http
    {
    public class CookieManager
    {
    private Dictionary cookieValues;

    public Dictionary CookieValues {
    get {
    if (this.cookieValues == null) {
    this.cookieValues = new Dictionary();
    }

    return this.cookieValues;
    }
    }

    public void PublishCookies(HttpWebRequest webRequest)
    {
    StringBuilder sb = new StringBuilder();
    sb.Append("Cookie: ");
    foreach (string key in this.CookieValues.Keys) {
    sb.Append(key);
    sb.Append("=");
    sb.Append(this.CookieValues[key]);
    sb.Append("; ");
    sb.Append("$Path=\"/\"; ");
    }

    webRequest.Headers.Add(sb.ToString());
    sb = null;
    webRequest = null;
    }

    public void StoreCookies(HttpWebResponse webResponse)
    {
    for (int x=0; x < webResponse.Headers.Count; x++) {
    if (webResponse.Headers.Keys[x].ToLower().Equals("set-cookie")) {
    this.AddRawCookie( webResponse.Headers[x] );
    }
    }

    webResponse = null;
    }

    private void AddRawCookie(string rawCookieData)
    {
    string key = null;
    string value = null;

    string[] entries = null;

    if (rawCookieData.IndexOf(",") > 0)
    {
    entries = rawCookieData.Split(',');
    }
    else {
    entries = new string[] { rawCookieData };
    }

    foreach (string entry in entries) {
    string cookieData = entry.Trim();

    if (cookieData.IndexOf(';') > 0)
    {
    string[] temp = cookieData.Split(';');
    cookieData = temp[0];
    }

    int index = cookieData.IndexOf('=');
    if (index > 0)
    {
    key = cookieData.Substring(0, index);
    value = cookieData.Substring(index + 1);
    }

    if (key != null && value != null)
    {
    this.CookieValues[key] = value;
    }

    cookieData = null;
    }

    rawCookieData = null;
    entries = null;
    key = null;
    value = null;
    }

    public override string ToString()
    {
    StringBuilder sb = new StringBuilder();
    sb.Append("[");
    foreach (string key in this.CookieValues.Keys) {
    sb.Append("{");
    sb.Append(key);
    sb.Append(",");
    sb.Append(this.CookieValues[key]);
    sb.Append("}, ");
    }
    if (this.CookieValues.Keys.Count > 0)
    {
    sb.Remove(sb.Length - 2, 2);
    }
    sb.Append("]");

    return sb.ToString();
    }
    }
    }




    Here is a usage example:


    CookieManager cookieManager = new CookieManager();
    // Set a cookie value
    cookieManager.CookieValues["FavoriteCookie"] = "Chocolate Chip";

    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
    // Publish the cookies to the request before asking for the response
    cookieManager.PublishCookies(webRequest);

    HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
    // Store any cookies returned from the response
    cookieManager.StoreCookies(webResponse);

    // Get the value of a cookie
    string session = cookieManager.CookieValues["SESSIONID"];

    webRequest = (HttpWebRequest)WebRequest.Create(url2);
    cookieManager.PublishCookies(webRequest);

    webResponse = (HttpWebResponse)webRequest.GetResponse();


  • 相关阅读:
    第一篇:fastadmin的页面是如何生成的?
    thinkphp join 表前缀
    python selenium firefox 添加cookie add_cookie
    python 多网站采集,解决编码问题
    How To Set Up vsftpd on CentOS 6
    How To Install Linux, nginx, MySQL, PHP (LEMP) stack on CentOS 6
    捕获浏览器的前进、后退事件 window.onhashchange 并区别于点击链接
    node之querystring
    详解html-webpack-plugin配置
    详解css-loader配置
  • 原文地址:https://www.cnblogs.com/jordan2009/p/1818319.html
Copyright © 2011-2022 走看看