好吧,所以我有一个我写的功能,应该允许我用cookie发布数据。问题是我正在亚马逊登录页面上测试它,并且它一直在回答说我需要启用cookie。这是代码
public string DoPost(String url, PostData data, CookieContainer cookies)
{
HttpWebRequest objWebRequest = (HttpWebRequest)WebRequest.Create(url);
objWebRequest.CookieContainer = cookies;
objWebRequest.AllowAutoRedirect = true;
objWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
if(data != null)
{
String post = data.ToString();
objWebRequest.Method = "POST";
objWebRequest.ContentLength = post.Length;
objWebRequest.ContentType = "application/x-www-form-urlencoded";
// Post to the login form.
using(StreamWriter swRequestWriter = new StreamWriter(objWebRequest.GetRequestStream()))
{
swRequestWriter.Write(post);
}
}
// Get the response.
HttpWebResponse objWebResponse =
(HttpWebResponse)objWebRequest.GetResponse();
// Read the response
using(StreamReader srResponseReader = new StreamReader(objWebResponse.GetResponseStream()))
{
string strResponseData = srResponseReader.ReadToEnd();
return strResponseData;
}
}
我称之为
String action = "https://www.amazon.com/gp/flex/sign-in/select.html";
String s = DoPost(action, null, Cookies);
Cookie是在我的类构造函数中创建的,如此
CookieContainer Cookies;
public Constructz0r()
{
Cookies = new CookieContainer();
}
问题是,我甚至没有发布任何帖子数据,我只是去页面,而且它说我的cookie没有启用,虽然我觉得我已经用DoPost写了。
我甚至尝试过使用WebClient的这种实现
public class CookieWebClient : WebClient
{
private CookieContainer _cookieContainer = new CookieContainer();
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
if (request is HttpWebRequest)
{
(request as HttpWebRequest).CookieContainer = _cookieContainer;
}
return request;
}
}
并称之为
using(CookieWebClient ck = new CookieWebClient())
{
String s = ck.DownloadString(action);
}
它仍然告诉我没有启用cookie。