Post method using cookie in C#
I try to get response from url but the program doesn't work. Can you see
it's and give me some hint ? Thanks very much !
// Create new CookieContainer
// Get the cookies
CookieContainer aCookieContainer = new CookieContainer();
CookieCollection cookie = new CookieCollection();
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(cookie);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
cookie = response.Cookies;
//Post method and receive from the website
string RequestUrl = "https://play.google.com/store/getreviews";
HttpWebRequest postRequest =
(HttpWebRequest)WebRequest.Create(RequestUrl.ToString());
postRequest.CookieContainer = aCookieContainer;
//getRequest.CookieContainer = new CookieContainer();
postRequest.CookieContainer.Add(cookie);
postRequest.Method = WebRequestMethods.Http.Post;
postRequest.Referer =
"https://play.google.com/store/apps/details?id=com.popcap.pvz_row&feature=top-paid";
postRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.22 Safari/537.36";
postRequest.Accept =
"text/html,application/xhtml+xml,application/xml;q=0.6,*/*;q=0.4"; ;
postRequest.AllowWriteStreamBuffering = true;
postRequest.ProtocolVersion = HttpVersion.Version11;
postRequest.AllowAutoRedirect = true;
postRequest.ContentType = "application/x-www-form-urlencoded";
postRequest.AutomaticDecompression = DecompressionMethods.GZip;
string postData =
String.Format("reviewType=0&pageNum=2&id=com.popcap.pvz_row&xhr=1");
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
getRequest.ContentLength = 50;
Stream newStream = getRequest.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);
newStream.Close();
HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();
using (StreamReader sr = new StreamReader( getResponse.GetResponseStream()))
{
string sourceCode = sr.ReadToEnd();
}
No comments:
Post a Comment