Need some advice. HTTPwebrequest and tokens.

Discussion in 'Visual Basic .NET' started by flann, Jun 18, 2012.

  1. flann

    flann Regular Member

    Joined:
    Jan 19, 2008
    Messages:
    208
    Likes Received:
    34
    I'm almost 2 months learning and building in VB. I know how to use the webbrowser control and make some tools with it. Now i need to switch over to httpwebrequest because of some things like multi-treading and so on. My problem is i can't find any tutorial or example how to use tokens. A simple login is not working without the specific tokens from the website i want to do my shit.

    Someone know a good tut or example where i can start? Maybe it is just 1 line of code but i can't get it to work.

    Is this something i can use?
    Code:
    Public Shared Sub AuthorizeRequest ( _
    	request As HttpWebRequest, _
    	accessToken As String _
    )
     
  2. theMagicNumber

    theMagicNumber Regular Member

    Joined:
    May 13, 2010
    Messages:
    347
    Likes Received:
    195
    Do you mean cookies or custom headers ?
    Share the website url and i will help you.
     
  3. flann

    flann Regular Member

    Joined:
    Jan 19, 2008
    Messages:
    208
    Likes Received:
    34
    P1nterest
    1=i
    ;)
     
  4. flann

    flann Regular Member

    Joined:
    Jan 19, 2008
    Messages:
    208
    Likes Received:
    34
    i found something:
    Code:
    Dim wpLoginToken As String = cutStr(HTML, "<input type=""hidden"" name=""wpLoginToken"" value=""", """") ' finding wpLoginToken parameter
            GetNewForm = wpLoginToken
    Not working but this is the right way.

    SOme code need to be executed before i send the POST data, so i can extract the token and use it. Right?
     
  5. flann

    flann Regular Member

    Joined:
    Jan 19, 2008
    Messages:
    208
    Likes Received:
    34

    Cookies (i think)
    csrfmiddlewaretoken=I-NEED-THIS
     
  6. laowai

    laowai Power Member

    Joined:
    Feb 27, 2011
    Messages:
    522
    Likes Received:
    185
    In high level: send first GET data, parse the token from the HTML and use it in your POST call.
     
  7. flann

    flann Regular Member

    Joined:
    Jan 19, 2008
    Messages:
    208
    Likes Received:
    34
    Sure, i know that. But i can't figure out how to do that.

    Low level would be great. ;)


    I can fix this with cookiecontainers?
    i found this, i wonder if i need to inspect this more or am i on the wrong way .
    Code:
    private CookieContainer m_CookieContainer = null;
     HttpWebRequest request = (HttpWebRequest)
     WebRequest.Create("http://yoursite.com/yourpage.aspx");
     request.CookieContainer = this.m_CookieContainer;
     request.Timeout = TimeOutTime;
     request.UserAgent = "whateveryouwanthere";
     request.Accept = "text/html";
     request.Referer = "whatever httpreferer if necessary"
     request.KeepAlive = true;
     request.MediaType = "text/html;";
    
     HttpWebResponse w = null;
     w = (HttpWebResponse) request.GetResponse();
     
  8. flann

    flann Regular Member

    Joined:
    Jan 19, 2008
    Messages:
    208
    Likes Received:
    34
    Found a better way:
    Code:
    url = "https://twitter.com/signup"
         HttpRequest = HttpRequest.Create(url)
         HttpRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3"
         HttpRequest.Method = "GET"
         HttpRequest.CookieContainer = cookie_holder
         HttpResponse = HttpRequest.GetResponse()

    But still can't get it working. Before my 1st "Dim postData As String =" i can add some code to GET the cookies and close that with "Next" so Next code will/can use the cookies?
     
    Last edited: Jun 18, 2012
  9. andee

    andee Regular Member

    Joined:
    Jul 24, 2010
    Messages:
    218
    Likes Received:
    83
    yeah with pinterest look at the headers and extra cookies that you need to make with your requests

    when i get around to it, im going to upload a picture pinning bot with source to learn off
     
  10. flann

    flann Regular Member

    Joined:
    Jan 19, 2008
    Messages:
    208
    Likes Received:
    34
    Great. Good luck with it.
     
  11. Chris22

    Chris22 Regular Member

    Joined:
    Sep 29, 2010
    Messages:
    400
    Likes Received:
    1,063
    Add a reference to HtmlAgilityPack to your project and use this, you'll need to convert it to VB.NET though.

    Code:
    HtmlDocument doc = new HtmlDocument();
    doc.LoadHtml( -- webpage html you downloaded as a string -- );
    string token = doc.DocumentNode.SelectSingleNode("//input[@name='csrfmiddlewaretoken']").Attributes["value"].Value;
    doc = null;