【问题标题】:Get cookie with WT_FPC (WebTrends tracking cookie).使用 WT_FPC(WebTrends 跟踪 cookie)获取 cookie。
【发布时间】:2012-07-18 20:09:46
【问题描述】:

使用 Fiddler 跟踪 POST 包,结果:

POST http://site1.do HTTP/1.1
Host: data.bls.gov
Connection: keep-alive
Content-Length: 12
Cache-Control: max-age=0
Origin: http://data.bls.gov
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko)     Chrome/20.0.1132.57 Safari/536.11
Content-Type: application/x-www-form-urlencoded
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Referer: http://site1.jsp
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: JSESSIONID=C6395D730AD166AF76AB4E1CC7ECC694.tc_instance3; WT_FPC=id=2859218d72fe8fdf53b1342567507813:lv=1342569213802:ss=1342567507813; fsr.s={"v":0,"rid":"1342592707969_345732","to":2.7,"f":1342594413334}

type=arg1

我必须保留 cookie 才能获取具有相同会话 ID 的另一个站点。 所以,我使用 WebClient 和扩展(CookieAwareWebClient 发布在 StackOverflow 上)。 一种方法是:

    private void ReadCookies(WebResponse r)
    {
        var response = r as HttpWebResponse;
        if (response != null)
        {
            CookieCollection cookies = response.Cookies;
            container.Add(cookies);
        }
    }

但是

response.Cookies = JSESSIONID=C6395D730AD166AF76AB4E1CC7ECC694.tc_instance3;

丢失了有关 WT_FPC 的信息。

【问题讨论】:

  • 您能详细说明一下吗?客户端是什么类型的应用程序(ASP.NET、桌面、Silverlight……)?你能展示你的完整客户端代码吗?
  • 真的,在代码中没有魔法。我的应用是桌面、控制台应用、c# 4.0。我使用 WebClient 扩展将值 POST/GET 值传入/传出站点。
  • 有人知道为什么 WT_FPC 的信息会在 HttpWebResponse 对象中丢失吗?我用 BugAid 搜索了所有 localc 等,没有地方可以找到 WT_FPC。
  • 您没有回答我关于您的完整源代码的请求。您在问题中提供的信息很难提供帮助。

标签: c# session c#-4.0 post cookies


【解决方案1】:

好的,这里有更多关于代码的信息。 所以 Webclient 扩展:

public class CookieAwareWebClient : WebClient
{
    public CookieAwareWebClient(CookieContainer container)
    {
        this.container = container;
    }

    private readonly CookieContainer container = new CookieContainer();

    protected override WebRequest GetWebRequest(Uri address)
    {
        WebRequest r = base.GetWebRequest(address);
        var request = r as HttpWebRequest;
        if (request != null)
        {
            request.CookieContainer = container;
        }
        return r;
    }

    protected override WebResponse GetWebResponse(WebRequest request, IAsyncResult result)
    {
        WebResponse response = base.GetWebResponse(request, result);
        ReadCookies(response);
        return response;
    }

    protected override WebResponse GetWebResponse(WebRequest request)
    {
        WebResponse response = base.GetWebResponse(request);
        ReadCookies(response);
        return response;
    }

    private void ReadCookies(WebResponse r)
    {
        var response = r as HttpWebResponse;
        if (response != null)
        {
            CookieCollection cookies = response.Cookies;
            container.Add(cookies);
        }
    }
}

这是客户端代码:

 using (CookieAwareWebClient client = new CookieAwareWebClient(Cookies))
        {
            string data = Encoding.Default.GetString(client.DownloadData(m_uri));
            try
            {
                NameValueCollection values = new NameValueCollection { { "type", "arg1" } };
                string URL1 = "http://x/search.jsp";

                client.Headers[HttpRequestHeader.KeepAlive] = "true";
                client.Headers[HttpRequestHeader.UserAgent] = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
                client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
                client.Headers[HttpRequestHeader.Accept] = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                client.Headers[HttpRequestHeader.AcceptEncoding] = "gzip,deflate,sdch";
                client.Headers[HttpRequestHeader.AcceptLanguage] = "en-US,en;q=0.8";
                client.Headers[HttpRequestHeader.CacheControl] = "max-age=0";

                byte[] result = client.UploadValues(URL1, "POST", values);
                string result1String = Encoding.UTF8.GetString(result);
                Console.WriteLine();
}

在 result1String 对象中,我有来自第一个网站的 html 网站。所以什么也没有发生。 我使用提琴手进行调查。并查看我的发送包不包含有关 WT_FPC 的信息。所以我尝试 fiddler composer 并使用该信息构建自己的包。网站反应很好!

所以,我尝试在代码信息中查找有关 WT_FPC 的信息,但没有。

【讨论】:

  • 谁应该设置这个WT_FPC cookie?您是否确保设置了 cookie?这是您对client.DownloadData(m_uri) 提出的第一个请求吗? m_uriURL1 是否在同一个域中?
  • 我必须设置 WT_FPC,但首先我必须在代码中找到它...我确保设置了 Cookie,但只有:JSESSIONID=C6395D730AD166AF76AB4E1CC7ECC694.tc_instance3。 m_uri 这里没关系。但是是的,这是同一个域。
  • 我不明白:你想在客户端上设置这个cookie还是从一个设置它的url中检索它?
  • 从 URL1 我想用 cookie (session + wt_fpc) 响应,所以在接下来的 POSTing 中我可以使用它。
  • 在 fiddler 中看到,发帖之间还有另一个站点,但首先我忽略了它,因为它是 img/gif 类型,但我仔细一看,发现有 cookie/登录
【解决方案2】:

客户网站http://site1.jsp 有我想要发布的表格。

<form name="searchForm" method="POST" **action="/oes/site1.do**">
  <table cellspacing="3" cellpadding="2" border="0" width="100%">
    <tr>
      <td>
        <P>
          <STRONG>Please select a search type</STRONG>:
        </P>
        <P>
          <INPUT type="radio" name="type" value="arg1" CHECKED />Multiple occupations for one geographical area
        </P>
        <P>
          <INPUT type="radio" name="type" value="occ_geo"/>One occupation for multiple geographical areas
        </P>
        <P>
          <INPUT type="radio" name="type" value="ind_occ"/>Multiple occupations for one industry
        </P>
        <P>
          <INPUT type="radio" name="type" value="occ_ind"/>One occupation for multiple industries
        </P>
      </td>
    </tr>
    <tr>
      <td>
        <input type="submit" value="Continue">
      </td>
    </tr>
    </table>
</form>    

【讨论】:

    猜你喜欢
    • 2019-12-02
    • 2011-01-26
    • 1970-01-01
    • 2012-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-26
    • 2013-08-27
    相关资源
    最近更新 更多