Post/Get HTTP Requests In VB.Net

Discussion in 'OT Technology' started by Fifafever, Apr 13, 2010.

  1. Fifafever

    Fifafever OT Supporter

    Joined:
    Jul 22, 2006
    Messages:
    5,606
    Likes Received:
    14
    Location:
    San Diego, Canada
    I'm trying to post information to a website, login, and then
    send a request with specific variables. I'm finding it really
    difficult to get information on how to do any of these things in
    vb.net so I'd really appreciate it if someone could point me in
    the right direction.

    Also, here's the php scrip I'm trying to emulate in vb.net.

    Code:
    require_once "HTTP/Request.php";
    $req = &new HTTP_Request('https://steamcommunity.com');
    $req->setMethod(HTTP_REQUEST_METHOD_POST);
    $req->addPostData("action", "doLogin");
    $req->addPostData("goto", "");
    $req->addPostData("steamAccountName", ACC_NAME);
    $req->addPostData("steamPassword", ACC_PASS);
    echo "Login: ";
    $res = $req->sendRequest();
    if (PEAR::isError($res))
    die($res->getMessage());
    $cookies = $req->getResponseCookies();
    if ( !$cookies )
    die("fail!\n");
    echo "ok\n";
    foreach($cookies as $cookie)
    $req->addCookie($cookie['name'],$cookie['value']);
    $mid = GetFriendID(MY_STEAM_ID);
    $fid = GetFriendID($invite_steam_id);
    $url = "
    http://steamcommunity.com/actions/G...
    type=groupInvite&inviter=$mid&invitee=$fid&group="
    . GROUP_ID;
    echo "Inviting $invite_steam_id ($fid): ";
    $req->setMethod(HTTP_REQUEST_METHOD_GET);
    $req->setUrl($url);
    $res = $req->sendRequest();
    if (PEAR::isError($res))
    die($res->getMessage());
    I've got Imports System.Web but I just get errors when I try and
    use System.Web.HttpRequest.

    Probably a simple problem but I'm new to .net and I've only had a formal education in VB6.
     
  2. djshotglass

    djshotglass New Member

    Joined:
    May 4, 2007
    Messages:
    3,795
    Likes Received:
    0
    I'm a vb6 user so I won't be of much help. But I've always found that http controls never did what I wanted. So I learned the http protocol and now just use a winsock control.

    The easiest thing for me to do is to packet log my browser while making the request I want. I then duplicate that request and remove the unnecessary headers. Send it to the http server and parse the response.
     
  3. CodeX

    CodeX Guest

    I just learned to do this for a program I wrote to automatically download images from a google image search or from deviantart... I had to figure out how to log in to deviantart.com with a username and password supplied in the program. Here is what I did...

    Code:
                'Open the address passed
                Browser.Url = New System.Uri("http://www.deviantart.com/")
    
                'Wait until the page loads completely
                While Not Browser.ReadyState = WebBrowserReadyState.Complete : My.Application.DoEvents() : End While
    
                'Locate the Username TextBox and automatically input your username  
                Dim theElementCollection As HtmlElementCollection = Browser.Document.GetElementsByTagName("Input")
                For Each curElement As HtmlElement In theElementCollection
                    Dim controlName As String = curElement.GetAttribute("name").ToString
                    If controlName = "username" Then
                        curElement.SetAttribute("Value", DA_UsernameBox.Text)
                    End If
                Next
    
                'Locate the Password TextBox and automatically input your password  
                theElementCollection = Browser.Document.GetElementsByTagName("Input")
                For Each curElement As HtmlElement In theElementCollection
                    Dim controlName As String = curElement.GetAttribute("name").ToString
                    If controlName = "password" Then
                        curElement.SetAttribute("Value", DA_PasswordBox.Text)
                    End If
                Next
    
                'Locate the "Sign In" Button and automatically click it  
                theElementCollection = Browser.Document.GetElementsByTagName("Input")
                For Each curElement As HtmlElement In theElementCollection
                    If curElement.GetAttribute("value") = "login" And curElement.GetAttribute("type") = "submit" Then
                        curElement.InvokeMember("click")
                        Exit For
                    End If
                Next
            End If
    
            While Not Browser.ReadyState = WebBrowserReadyState.Complete : My.Application.DoEvents() : End While
    I copied this from a tutorial and it could obviously be optimized, but I just left it as is for my retarded uses. :rofl:

    At the most basic level you load the page using a .NET WebBrowser object, wait for it to load, then get and iterate through the different tag elements in the document until you find the one that matches what you want. Then you use the SetAttribute or InvokeMember functions of the HtmlElement object to do whatever you wanna do with it.
     
    Last edited by a moderator: Apr 15, 2010
  4. Fifafever

    Fifafever OT Supporter

    Joined:
    Jul 22, 2006
    Messages:
    5,606
    Likes Received:
    14
    Location:
    San Diego, Canada
    Thanks for the code snippet but I was hoping to avoid using any controls, it's a console application too.
     
  5. CodeX

    CodeX Guest

    Okay, but mind if I ask why? The only control you need to use there is the WebBrowser class, and that does not need a GUI, it can be used in a console based application, or even a service or DLL...
     
  6. telc

    telc OT Supporter

    Joined:
    Aug 29, 2003
    Messages:
    18,580
    Likes Received:
    0

Share This Page