POST(登录)后获取请求
GET Request after POST(login)
我有一个网站,我每天都会查看我的产品列表。我想为它做一个桌面程序。
我需要先登录该网站,然后才能访问网站。com/v1/ProductList 这是一个 xml 文档。我已成功使用此代码登录:
CookieCollection cookies = new CookieCollection();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(FirstURL);
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(cookies);
//Get the response from the server and save the cookies from the first request..
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
cookies = response.Cookies;
string postData = "Username=x&Password=x&List=1&Submit=Submit";
HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(getUrl);
getRequest.CookieContainer = new CookieContainer();
getRequest.CookieContainer.Add(cookies); //recover cookies First request
getRequest.Method = WebRequestMethods.Http.Post;
getRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
getRequest.AllowWriteStreamBuffering = true;
getRequest.ProtocolVersion = HttpVersion.Version11;
getRequest.AllowAutoRedirect = true;
getRequest.ContentType = "application/x-www-form-urlencoded";
byte[] byteArray = Encoding.ASCII.GetBytes(postData);
getRequest.ContentLength = byteArray.Length;
Stream newStream = getRequest.GetRequestStream(); //open connection
newStream.Write(byteArray, 0, byteArray.Length); // Send the data.
newStream.Close();
HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();
using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
{
string sourceCode = sr.ReadToEnd();
}
至此我登录成功了。
但是在此之后,如果我为我的列表(站点。com/v1/ProductList)创建一个新的获取请求并获取请求,它会将我重定向到登录页面。
编辑:我刚刚意识到我登录后无法获得任何 cookie。它说“'enumeration yielded no results'”。
我现在不知道如何解决它。
谢谢
更改此行:
getRequest.AllowAutoRedirect = true;
对此:
getRequest.AllowAutoRedirect = false;
确保您确实将其设置为 false
而不要只是删除该行,因为默认情况下,它会设置为 true
.
我有一个网站,我每天都会查看我的产品列表。我想为它做一个桌面程序。
我需要先登录该网站,然后才能访问网站。com/v1/ProductList 这是一个 xml 文档。我已成功使用此代码登录:
CookieCollection cookies = new CookieCollection();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(FirstURL);
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(cookies);
//Get the response from the server and save the cookies from the first request..
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
cookies = response.Cookies;
string postData = "Username=x&Password=x&List=1&Submit=Submit";
HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(getUrl);
getRequest.CookieContainer = new CookieContainer();
getRequest.CookieContainer.Add(cookies); //recover cookies First request
getRequest.Method = WebRequestMethods.Http.Post;
getRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
getRequest.AllowWriteStreamBuffering = true;
getRequest.ProtocolVersion = HttpVersion.Version11;
getRequest.AllowAutoRedirect = true;
getRequest.ContentType = "application/x-www-form-urlencoded";
byte[] byteArray = Encoding.ASCII.GetBytes(postData);
getRequest.ContentLength = byteArray.Length;
Stream newStream = getRequest.GetRequestStream(); //open connection
newStream.Write(byteArray, 0, byteArray.Length); // Send the data.
newStream.Close();
HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();
using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
{
string sourceCode = sr.ReadToEnd();
}
至此我登录成功了。 但是在此之后,如果我为我的列表(站点。com/v1/ProductList)创建一个新的获取请求并获取请求,它会将我重定向到登录页面。
编辑:我刚刚意识到我登录后无法获得任何 cookie。它说“'enumeration yielded no results'”。
我现在不知道如何解决它。
谢谢
更改此行:
getRequest.AllowAutoRedirect = true;
对此:
getRequest.AllowAutoRedirect = false;
确保您确实将其设置为 false
而不要只是删除该行,因为默认情况下,它会设置为 true
.