Post Form Data With Inet Control

This is funny.  I just googled how to do this and found 10 message boards with the question, but none with the answer!  LOL  

I need to post form data to a URL via an Internet Transfer Control (Inet).  I can see from Intellisense that I need to change the Inet.Execute statement, but I cannot figure out what I am supposed to fill in for the operation, input data,. and input headers (I think it says it wants hdrs, or headers).   This is the general format:
'***Inet1.Execute URL, <operation>, <input data>, <input hdrs>
'Form data fields.  I am not sure if these are formatted correctly.  Seems like I should need the form name...but I don't know what I am doing here.
PostData = "imgsize=320;opt=-l;lat=22.125;ns=North;lon=99.625;ew=West;alt=148024950;img=learth.evif"
'This execute statement bombs, saying it "cannot coerce type."
Inet1.Execute ", PostForm, PostData

Unfortunately, I do not have a good VB reference, so I could not look this up.  I feel like an idiot, but really appreciate any help people can send along.

Thank you.
Who is Participating?
JohnBPriceConnect With a Mentor Commented:
Hi Danimal,

There are basically two ways to post data, one is to use HTTP POST operations, and one is to use HTTP GET.  With POST, the data is posted as individual controls on a web form, with GET, the data is passed in the URL itself.  Your code above is trying to do a POST.  POST has always been dicey for me, but if you look at the code I posted in your earlier question,, (thanks for the points BTW), you'll see I was actually doing a GET (which is the default if you don't explicitly specify "POST", thus, the data is actually appended to the URL after the "?", as in

Inet1.Execute "" & PostData

Sorry for the confusion, I should not have named the variable "PostData", perhaps "URLParams" would have less misleading.  I actually tried POST first, but couldn't figure it out, and I noticed some of the samples did a GET, so GET was easier.

This particular site appears to accept both POST and GET, and since GET is simpler, I used GET.  Also note that your code has a couple issues if you really want to do a post, first, your do not want the "?" in the url, because POST doesn't use it.  Second, you must use the word "POST" as the second parameter, e.g. the <operation>, which I believe you missed in your actual code.
DanimalAuthor Commented:
IThank you John.  I understand what you are saying.  

If you look at the value of PostData in the answer your provided, there are semicolons.  Taking them out seems to have fixed this.

I need to work with this a little

DanimalAuthor Commented:
removing the semi colons solved the problem.  

I have a new problem though.  The download of the picture does not always work.  I am not changing anything.  Just running, deleting the files downloaded, running again..over and over.  Most of the time it works, but not always.  Any thoughts on that?  When it doesn't work, the picture .jpg file downloads as 0 bytes.  I am saving the URL of the photos.  The URLs are good, but the picture file building just doesn't happen everytime.  It seems as if the server is rejecting the URL, but like I said, I save and test those... so I don't know.
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

ooops, as I said "I actually tried POST first, but couldn't figure it out", I must have cut and paste incorrectly.
when it fails, take a look at the headers you receive, they might give you a clue as to why it failed.  You can get the headers using Inet1.GetHeader, which with no parameters, returns all the headers, including the HTTP success/error code.

Also, you can check if you get no data before saving, just check ubound(Image) where Image is the byte array you filled with GetChunk.
DanimalAuthor Commented:
ok..taking a break now... but I will see what is up.

thax again..and have a happy thanksgiving.
DanimalAuthor Commented:
John, after executing Inet1.GetHeader, how do you usually examine the results coming back.  I really don't have a lot of experience with VB, so I am looking for some little tricks.  I assume there are a lot of using the debugger, or writing the results to a text file... can you offer a few off the top of your head tips?

Ditto for the unbound(Image)... and the GetChunk command.  

I think I can work this through.  I am looking for tips I might pick up if i were looking over your shoulder when you were programming.

If I don't talk to you sooner, Happy Thanksgiving.
Yep, a lot of options.  I think perhaps the best for this site is to check the http return code in the header.  If it is HTTP OK, then you can assume you got the image.  Note once you get a look at all the headers with GetHEader, you can fetch only the one you want with GetHeader("HeaderNAme").  I forget exactly what the return code is named, but you will see it in GetHeader.

If you want to see the value of a property, you can just hover over it and VB will show you the value (but not getHeader, it is a method not a property).  If I just want to see GetHeader once in debug mode, I'll go to the immediate command window and do "?inet1.GetHeader".  The "?" is shorthand for "Print".  If I want to monitor some value, I'll add a watch.  Highlight the variable or expression, such as "Inet1.GetHeader", right click and "add watch".  You might want to simply choose "Inet1", and the watch will show you all the variables (but not the method calls like GetHeader).  The watched values update automatically as you step through the code.

You can do the same for ubound(Image), however GetChunk istelf is dicier because it is a byte array, not a string.  Also, you can not call it repeatedly and get the same results (it's pulls the chunk out after you get it).  So if you want to see the value, you have to look at the resulting Image byte array, not that it would do you much good since it is a JPG binary data.

If it is going to be an unattended process, and I want to track how it works, I'll write out bits to a log file.  Here is the core stuff from my logging routine, so that I can simply call

LogMessage "blah blah blah " & inet1.Getheader

or whatever you want to include in the logfile

Public Sub LogMessage(msg As String, Optional SuppressTimeStamp As Boolean)
    'write a new message to the current log file, create a new log file is one is not currently open
    If LogFile Is Nothing Then 'logfile not created yet
        Set LogFile = fs.OpenTextFile(App.Path & "\logfile.txt", ForWriting, True)
        LogFile.WriteLine "Logfile  created " & Now
    End If
    If msg <> "" Then
        If SuppressTimeStamp Then
            LogFile.WriteLine (msg)
            LogFile.WriteLine (Format(Now(), "Medium Time") & " - " & msg)
        End If
        LogFile.WriteBlankLines 1
    End If
End Sub

DanimalAuthor Commented:
WOW!  What a great response... I am in awe, and I am not worthy.  Thank you a million times!
Thank you 1
Thank you 2
Thank you 3
Thank you 4
Thank you 5
Thank you 6
Thank you 7
Thank you 8
Thank you 9
Thank you 10
Thank you 11
Thank you 12
Thank you 13
Thank you 14
Thank you 15
Thank you 16
Thank you 17
Thank you 18
Thank you 19
Thank you 20
Thank you 22
Thank you 23
Thank you 24
Thank you 25
Thank you 26
Thank you 27
Thank you 28
Thank you 29
I hope you get the idea.  :-)  I really appreciate the help.  :-)  Thank you.  You are my hero.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.