我的问题是如何进行POST请求,然后从该请求获取cookie并将其设置为C#中的示例GET类型的下一个请求?请举例说明。
我曾尝试过很多次,例如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请求登录,接下来例如10-20请求POST / GET由第一个请求(登录)的身份验证cookie
我这样做:
//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)