Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获取cookie并将其恢复到下一个请求?_C#_Cookies_Httpwebrequest_Webclient - Fatal编程技术网

C# 如何获取cookie并将其恢复到下一个请求?

C# 如何获取cookie并将其恢复到下一个请求?,c#,cookies,httpwebrequest,webclient,C#,Cookies,Httpwebrequest,Webclient,我的问题是如何进行POST请求、next从该请求获取cookie并将其设置为next请求(例如C#中的get type)?请举例说明代码 我尝试过很多次,例如CookieAwareWebClient方法,但都不起作用: public class CookieAwareWebClient : WebClient { public CookieContainer CookieContainer { get; set; } public CookieAwareWebClient()

我的问题是如何进行POST请求、next从该请求获取cookie并将其设置为next请求(例如C#中的get type)?请举例说明代码

我尝试过很多次,例如CookieAwareWebClient方法,但都不起作用:

public class CookieAwareWebClient : WebClient
{
    public CookieContainer CookieContainer { get; set; }

    public CookieAwareWebClient()
        : base()
    {
        CookieContainer = new CookieContainer();
    }

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

        HttpWebRequest webRequest = request as HttpWebRequest;
        if (webRequest != null)
        {
            webRequest.CookieContainer = CookieContainer;
        }

        return request;
    }
}
我需要什么?我需要通过POST请求登录,然后通过第一个请求的authenticate cookie(登录)执行下一个操作,例如10-20请求POST/GET

我是这样做的:

//Create an instance of your new CookieAware Web Client
using (var client = new CookieAwareWebClient())
{
    //Authenticate (username and password can be either hard-coded or pulled from a settings area)
    var values = new NameValueCollection{{ "UserName", username },{ "Password", password }};

     client.UploadValues(new Uri("http://www.yourdomain.com/Account/LogOn/"), "POST", values);

     client.UploadString(new Uri("http://www.yourdomain.com/Secure/YourSecureMethod"), 
 "POST", "Example Message");           
}

但是我无法将此cookie设置为我的下一个请求(下一个请求是创建他们自己的新的未经身份验证的cookie)

“尝试了很多”不会给你任何机会帮助你解决问题。示例显示2个带有读/写标题的请求应该很小,可以显著改进您的帖子。。。根据你们实际想做什么,它甚至可能会在之前得到回答…谢谢你们的样品。。。但是你也需要用示例URL显示用法(示例域部分可能足够长,只要它显示哪些域是相同的,哪些域是不同的),以便有人看到它失败的原因。你看过了吗?我已经编辑了你的标题。请参阅“”,其中的共识是“不,他们不应该”。