Jump to content

WinHttp.WinHttpRequest.5.1 help


toxicvn
 Share

Recommended Posts

I'm trying to write code to do a POST HTTP Request to a website. The POST HTTP Request with their API looks like this:

<form method='post' 
    action='https://www.sysdevx.com/api/GetAllVehicles.php'>
  <input type='text' name='login' value='your_login' />
  <input type='text' name='password' value='your_password' />
  <input type='submit'>
</form>

Here is a section with my corresponding code:

hr = CLSIDFromProgID(L"WinHttp.WinHttpRequest.5.1", &clsid);

        if (SUCCEEDED(hr))
        {
            hr = CoCreateInstance(clsid, NULL, 
                                  CLSCTX_INPROC_SERVER, 
                                  IID_IWinHttpRequest, 
                                  (void **)&pIWinHttpRequest);
        }
        if (SUCCEEDED(hr))
        {   // Open WinHttpRequest.
            BSTR bstrMethod  = SysAllocString(L"POST");
            BSTR bstrUrl = SysAllocString(L"https://www.sysdevx.com/api/GetAllVehicles.php");       
            hr = pIWinHttpRequest->Open(bstrMethod, bstrUrl, varFalse);
            SysFreeString(bstrMethod);
            SysFreeString(bstrUrl);
        }
        if (SUCCEEDED(hr))
        {   // Send Request.

            pIWinHttpRequest->SetRequestHeader("Login", "my_login");
            pIWinHttpRequest->SetRequestHeader("Password", "my_password");
            
            hr = pIWinHttpRequest->Send(varEmpty);
        }
        if (SUCCEEDED(hr))
        {   // Get Response text.
            hr = pIWinHttpRequest->get_ResponseText(&result);   
        }
        
        // Print response to console.
        wprintf(L"%.256s",result);

The response is an XML response:

<?xml version ="1.0" encoding = "UTF-8"?>

<response><status code = "103" msg="No devices for this account"/></response>

Am I not sending the username/password correctly? Should I be using SetCredentials instead of SetRequestHeader? I'm not sure if my Send is correct either. I do know however that while this is a valid response, it is the incorrect one because I could just take out the username/pwd lines and I still get the above response. Also, my response is different when I test it on their website forms....

Anyway, what am I doing wrong here? Let me know if you need more info but I'm basically just trying to see if I'm doing an HTTP POST correctly. Thanks.

Link to comment
Share on other sites

I'm trying to write code to do a POST HTTP Request to a website. The POST HTTP Request with their API looks like this:

<form method='post' 
    action='https://www.sysdevx.com/api/GetAllVehicles.php'>
  <input type='text' name='login' value='your_login' />
  <input type='text' name='password' value='your_password' />
  <input type='submit'>
</form>

Here is a section with my corresponding code:

CODE
hr = CLSIDFromProgID(L"WinHttp.WinHttpRequest.5.1", &clsid);

if (SUCCEEDED(hr))

{

hr = CoCreateInstance(clsid, NULL,

CLSCTX_INPROC_SERVER,

IID_IWinHttpRequest,

(void **)&pIWinHttpRequest);

}

if (SUCCEEDED(hr))

{ // Open WinHttpRequest.

BSTR bstrMethod = SysAllocString(L"POST");

BSTR bstrUrl = SysAllocString(L"https://www.sysdevx.com/api/GetAllVehicles.php");

hr = pIWinHttpRequest->Open(bstrMethod, bstrUrl, varFalse);

SysFreeString(bstrMethod);

SysFreeString(bstrUrl);

}

if (SUCCEEDED(hr))

{ // Send Request.

pIWinHttpRequest->SetRequestHeader("Login", "my_login");

pIWinHttpRequest->SetRequestHeader("Password", "my_password");

hr = pIWinHttpRequest->Send(varEmpty);

}

if (SUCCEEDED(hr))

{ // Get Response text.

hr = pIWinHttpRequest->get_ResponseText(&result);

}

// Print response to console.

wprintf(L"%.256s",result);

The response is an XML response:

<?xml version ="1.0" encoding = "UTF-8"?>

<response><status code = "103" msg="No devices for this account"/></response>

Am I not sending the username/password correctly? Should I be using SetCredentials instead of SetRequestHeader? I'm not sure if my Send is correct either. I do know however that while this is a valid response, it is the incorrect one because I could just take out the username/pwd lines and I still get the above response. Also, my response is different when I test it on their website forms....

Anyway, what am I doing wrong here? Let me know if you need more info but I'm basically just trying to see if I'm doing an HTTP POST correctly. Thanks.

What did this have to do with AutoIt?

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...