Results 1 to 11 of 11

Thread: [RESOLVED] Working with MSXML Post

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Location
    Texas
    Posts
    162

    Resolved [RESOLVED] Working with MSXML Post

    Im getting a "User-defined type not defined" error I do not understand when trying to use the MSXML Post function below. I have made sure I have added the Project Reference for the MSXML 3.0 (but also tried v 6.0). It highlights the New MSXML.XMLHTTPRequest section. I've tried to Dim myMSXML As String but no go. So I'm obviously missing something here,

    Set myMSXML = New MSXML.XMLHTTPRequest
    myMSXML.Open "POST", "https://abc.com/test/, True"
    myMSXML.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    myMSXML.setRequestHeader "User-Agent", "Firefox 3.6.4"
    myMSXML.Send "caption=" & abc.Caption & "&date= " & Format$(Date, "Short Date") & "time=" & Format(Time, "Long Time")

  2. #2
    Fanatic Member
    Join Date
    Jan 2013
    Posts
    759

    Re: Working with MSXML Post

    Check your Project for missing or broken References.

    I've often found that a completely unrelated library can be missing and causes things to breaks in "interesting" and "unexpected" ways.

    Regards, Phill W.

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: Working with MSXML Post

    I'll admit it's been sometime since I've done VB6 ... but shouldn't this:
    Code:
    Set myMSXML = New MSXML.XMLHTTPRequest
    be this:
    Code:
    Set myMSXML = New MSXML.XMLHTTPRequest()

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Location
    Texas
    Posts
    162

    Re: Working with MSXML Post

    No thats not it.

  5. #5
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: Working with MSXML Post

    Try using MSXML2 and the XMLTTP object...

    Code:
    ' Set myMSXML = New MSXML.XMLHTTPRequest
    
    set myMSXML As New MSXML2.XMLHTTP

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Location
    Texas
    Posts
    162

    Re: Working with MSXML Post

    That got past the user-defined error but the post is still not happening so let me play some more. Thanks.

  7. #7
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: Working with MSXML Post

    See if this is any help - it's Excel, but the syntax should still be the same. There's a POST example a little way down.
    https://codingislove.com/http-requests-excel-vba/

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Working with MSXML Post

    Your web bot code is lying. It claims to be sending x-www-form-urlencoded content yet it just sends plain text.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Location
    Texas
    Posts
    162

    Re: Working with MSXML Post

    I just discovered the exact code that was provided to me and it was actually something that was post back in 2010 on another forum (https://stackoverflow.com/questions/...-to-remote-url). But there are parts of this that I don't understand.

    If you want to do it simple and sweet, XML HTTP Request is the way to go, I use it in javascript too. Try something like:

    Set myMSXML = New MSXML.XMLHTTPRequest
    myMSXML.open "POST", URL, True
    myMSXML.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    myMSXML.setRequestHeader "User-Agent", "Firefox 3.6.4"
    myMSXML.OnReadyStateChange = (Shown below)
    myMSXML.send YourPostDataString


    And the OnReadyStateChange function:

    Dim HttpResponse As String

    HttpResponse = myMSXML.responseText


    So what I am not clear on is the last part of the OnReadyStateChange function and above where it references (Shown below). Where does that go? I'm lost on that part.

  10. #10
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Working with MSXML Post

    Original text:

    Code:
    caption=Blah € blah&date=5/20/2022&time=10:14:23 PM
    After encoding for x-www-form-urlencoded (but incorrect):

    Code:
    caption=Blah%20€%20blah&date=5%2F20%2F2022&time=10%3A14%3A23%20PM
    Encoded correctly:

    Code:
    caption=Blah%20%E2%82%AC%20blah&date=5%2F20%2F2022&time=10%3A14%3A29%20PM

    The original encoding has illegal "/" and ":" and " " characters in it, as well as the Euro symbol.

    The first attempt above corrects for some of the illegal characters, but incorrectly passes through the Euro symbol.

    In earlier times that approach worked ok as long as the original text was made up of strictly ASCII characters from 0 to 127. However foreigners wanted in too and corporations wanted to sell them computers. Now a huge character set must be accommodated, and over time most web servers went along with this.

    So today almost all web servers expect UTF-8 instead of ASCII, though illegal characters must still be %-encoded byte by byte.

    To do this right each key (field name) and value must separately be URL-encoded in this way, and only then strung together with "=" joining each key and value as well as "&" joining those pairs together.

    Another issue is that when you pass a String to the send() method, it must be encoded as UTF-16 characters. These are normal VB characters, but if you translated to UTF-8 before %-encoding you must now translate back, or else pass a Byte array of UTF-8 data to send().


    In the most degenerate cases where the keys and values only use legal characters such as letters and digits no real effort is required. But you have "/" and ":" and " " so you are already in trouble and must percent-encode. Cases that add characters beyond ASCII 126 (like that Euro sign) require more effort because UTF-8 character encoding is required before %-encoding.

    The precise rules vary because the original specification is vague and was modified later for non-ASCII characters. Not every web server follows exactly the same rules because not all browsers do. So some servers accept a relaxed set of rules, though all should accept the most specific approach given above.


    Your problem isn't coming from doing synchronous requests. That's a different issue.

    The OnReadyStateChange thing you copy/pasted won't work in VB6 as shown. It appears to be Javascript some script kiddie crudely translated into pidgin-VB and never actually tested. The XmlHttpRequest class implemented onReadyStateChange as a late-bound object reference, not a proper COM event or callback function.

    As the documentation states:

    Create a wrapper class, and create a procedure to handle the event within the class module. Set the procedure to the default, and bind the class to the onreadystatechange event to either the IXMLHTTPRequest or IServerXMLHTTPRequest component, depending on which component you are using with your application.
    See: Use the onReadyStateChange Property (Visual Basic)

    There is a discussion there, sample code, and step by step instructions. Hilariously it looks like somebody at Microsoft copied my own code and steps from a CodeBank thread here. It wasn't there just a year ago.


    MSXML2 was designed to be able to support browser-hosted script, not VB programs. It jumps through a lot of hoops to be usable from Javascript.

    The WinHttpRequest does not have this baggage, so it implements a proper COM event for monitoring async request progress. As a result it is easier to use in a VB program.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Location
    Texas
    Posts
    162

    Re: Working with MSXML Post

    None of the above helped solve the issue, but I did find additional sample someone posted at the site where I found the original person who gave me the sample had cut/pasted from which did solve the issue.

    Set myMSXML = CreateObject("Microsoft.XmlHttp")
    myMSXML.open "POST", "http://....", False
    myMSXML.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    myMSXML.setRequestHeader "User-Agent", "Firefox 3.6.4"
    myMSXML.send "param1=value2&param2=value2"
    MsgBox myMSXML.responseText

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width