Reputation: 207
im building an app that after the the user logs in to my web site the app goes to a link to get his user name so i will recognize him on the app.
Now if i as a user login from a browser and then paste the like on that browser i will get a page with my username but if i do a web request from code i get an empty page.
My question is how to open the url as a browser or how can i get the value of the cookie on a certin browser
i have tryied
string s= GetHtmlPage("http://www.somedomain.com/account/show_cookies.php","Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US)");
static string GetHtmlPage(string strURL,string browser)
{
String strResult;
System.Net.WebResponse objResponse;
System.Net.WebRequest objRequest = System.Net.HttpWebRequest.Create(strURL);
((System.Net.HttpWebRequest)objRequest).UserAgent =browser;
objResponse = objRequest.GetResponse();
using (System.IO.StreamReader sr = new System.IO.StreamReader(objResponse.GetResponseStream()))
{
strResult = sr.ReadToEnd();
sr.Close();
}
return strResult;
}
But that also return a blank page.
Upvotes: 3
Views: 11316
Reputation: 309
You need to use HttpClient and CookieContainer
CookieContainer cookies = new CookieContainer();
HttpClientHandler handler = new HttpClientHandler();
handler.CookieContainer = cookies;
HttpClient client = new HttpClient(handler);
HttpResponseMessage response = client.GetAsync("http://google.com").Result;
Uri uri = new Uri("http://google.com");
IEnumerable<Cookie> responseCookies = cookies.GetCookies(uri).Cast<Cookie>();
foreach (Cookie cookie in responseCookies)
Console.WriteLine(cookie.Name + ": " + cookie.Value);
Console.ReadLine();
Upvotes: 1