带代理的HttpWebRequest - 设置cookie

时间:2012-02-20 10:11:35

标签: c# cookies

这是我的代码:

        var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost/jsonrpc.cgi");
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = "POST";
        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            string json = "someParameters";
            streamWriter.Write(json);
        }
        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var responseText = streamReader.ReadToEnd();
        }

        string Bugzilla_logincookie= httpResponse.Headers.ToString();
        Bugzilla_logincookie= Bugzilla_logincookie.Substring(plsWork .IndexOf("logincookie") + 12);
        Bugzilla_logincookie= Bugzilla_logincookie.Substring(0, plsWork .IndexOf(";"));

        CookieContainer cc = new CookieContainer();
        cc.SetCookies(new Uri("http://localhost"), Bugzilla_logincookie);
        var httpWebRequest2 = (HttpWebRequest)WebRequest.Create("http://localhost/jsonrpc.cgi");
        httpWebRequest2.ContentType = "application/json";
        httpWebRequest2.Method = "POST";
        httpWebRequest2.Proxy.Credentials = new NetworkCredential("username", "password");
        httpWebRequest2.CookieContainer = cc;

        using (var streamWriter2 = new StreamWriter(httpWebRequest2.GetRequestStream()))
        {
            string json = "someParametersForJsonCall";
            streamWriter2.Write(json);
        }
        var httpResponse2 = (HttpWebResponse)httpWebRequest2.GetResponse();
        using (var streamReader2 = new StreamReader(httpResponse2.GetResponseStream()))
        {
            var responseText = streamReader2.ReadToEnd();
        }

我使用代理有问题。我要做的是:使用http://www.bugzilla.org/docs/tip/en/html/api/Bugzilla/WebService/User.html的代理来调用登录方法,然后存储响应的cookie,并在每次调用会话时发送它们。 我收到这个错误:

"You must log in before using this part of Bugzilla."

我错误地使用了什么?

0 个答案:

没有答案