0

There is this trend of cookies not turning up. I copy fiddler exactly and it should be a simple task since it's handled automatically anyway. Why am I not getting proper cookie retrieval?

public class HTTP : WebClient
{
    public HTTP()
        : this(new CookieContainer())
    { }

    public HTTP(CookieContainer c)
    {
        CookieContainer = c;
    }
    public CookieContainer CookieContainer { get; set; }

    protected override WebRequest GetWebRequest(Uri address)
    {
        WebRequest request = base.GetWebRequest(address);

        var castRequest = request as HttpWebRequest;
        if (castRequest != null)
        {
            castRequest.CookieContainer = CookieContainer;
            castRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            castRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22";

        }

        return request;
    }
} 


public void checkUrl()
{
CookieContainer cookieJar = new CookieContainer();
HTTP client = new HTTP(cookieJar);

string responseData = client.DownloadString(url);
       responseData = client.UploadString(url, loginData);
rtb.Text = responseData;
}
4

1 回答 1

0

You probably want to hook into the WebResponse and read the cookies from there. The request does not change after it is sent.

于 2013-03-26T21:24:06.183 回答