0

使用 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 的信息。

4

2 回答 2

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 的代码信息中查找,但没有。

于 2012-07-18T10:09:21.167 回答
0

客户站点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>    
于 2012-07-18T12:09:46.820 回答