使用Fidller跟踪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和扩展(在StackOverflow上发布CookieAwareWebClient)。 一种方法是:
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的信息丢失。
答案 0 :(得分: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网站。没有任何事情发生 我用fiddler来调查。并看到我的发送包doeasnt包含有关WT_FPC的信息。所以我尝试fiddler composer并使用该信息构建自己的包。并且网站回复很好!
所以,我尝试查找有关WT_FPC的代码信息,但是没有。
答案 1 :(得分: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>