-2

I'm currently working on a project in C#. So basically I'm checking account existence, however I'm not sure the best route to go about it.

So basically to begin I'm need to go to the root of the website (http://domain.com/). The reason I need to go to the root of the website is to correctly set the cookies. Than I need to "simulate" a click on the anchor of the sign in.

However I don't want to use a Web Browser due to them being incredibly slow. I'm wanting to use HttpWebRequests to send the POST request. But I don't believe I can "click" the sign in link from the root of the website.

Therefor I'm wondering if this is possible and how I could do so using HttpWebRequests. I know using C# WebBrowser I can use InvokeMember by ID.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Varian
  • 1
  • 2
  • What does the sign in anchor do? Is it just a POST to another page? – Andrew Jan 18 '16 at 00:55
  • No sorry my bad. Anchor tag is the tag for a link (). It has not POST, it just changes the URL to the href. – Varian Jan 18 '16 at 00:59
  • 1
    Use a `CookieContainer` for cookies, HtmlAgilityPack to find the link for the sign-in, make sure you set the `Referrer` header correctly, and use fiddler if anything doesn't work to find the difference. – Mitch Jan 18 '16 at 01:32
  • It is very strange that while searching for slution you did not find any existing answers - consider using Bing - https://www.bing.com/search?q=c%23+webrequest+signin (or Google) instead of search engine you are using currently. If duplicate did not provide all code you needed - feel free to ask new clarifying questions. – Alexei Levenkov Jan 18 '16 at 01:55

1 Answers1

0

If the only issue is getting the cookies set by the main page, it looks like the CookieContainer will get you at least most of the way there. Make a GET request to the root page to get the cookies set, then use the same container to make the request to the sign-in page.

To get the sign-in URL (with the query parameters) you can parse the response. If it's all stored in the response's link attribute, it could be as simple as scanning the source for a tag with the right id and finding the link attribute. See the HttpWebResponse.GetResponseStream method.

However if this is a professional product, it's best to go with a real HTML parser. It looks like the HTML Agility Pack is a popular one. From this answer it looks like you can feed the stream directly into there and then use it's API to search for the element.

If the link triggers some JavaScript to come up with the URL/parameters, you're best off reading (and probably beautifying) the JavaScript to see what it does.

Community
  • 1
  • 1
Andrew
  • 1,839
  • 14
  • 25
  • That sounds like a brilliant solution, however when you go to the main website it sets cookies and the sign in parameters. Meaning you can't just go to http://domain.com/signin, it has a whole bunch of parameters and if you try to go to the root of the file the request is rejected. – Varian Jan 18 '16 at 01:12
  • Added more, let me know if that helps. – Andrew Jan 18 '16 at 01:32