Avatar of Rabbit80
Rabbit80Flag for United Kingdom of Great Britain and Northern Ireland asked on

http POST in Visual Basic

I am trying to learn how to perorm a http POST from within a VB application. The idea is to upload a file  (which will be PDF) and get back a result which i then use later.

The upload url is http://server/webservices/upload.php
Required info is Filename and SessionID both of which I know.

How do I do this in Visual Basic 2008?
Visual Basic Classic

Avatar of undefined
Last Comment
Rabbit80

8/22/2022 - Mon
SStory

ASKER
Rabbit80

thanks, these links are helping. The problem I am facing at the moment is that I am getting a "session not specified" error message. I dont know where and how to add this info. I have created a Session_ID (There was a mistake in my first post - needs the underscore) using SOAP, but I now need to pass this back to the upload.php along with filename and the binary data.

I dont understand fully how the POST syntax is constructed - is there any chance you could show me what the completed POST that I send should look like?
ASKER
Rabbit80

If it helps, the following link is the source for the uploads.php script;

http://docs.knowledgetree.com/api/__filesource/fsource_KTWEBSERVICE__ktwebserviceupload.php.html 
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
SStory

hmm.. So are do you have a page, with no login, and you are wanting to use the sessionID to uniquely identify the user?  If so, I don't think sessionID's are unique for all time.  
I guess I am thinking ASP/ASP.NET when you say session ID.  IS this an ASP.NET (vb.net) app, or just a plain Windows Forms Vb.net app?
ASKER
Rabbit80

Let me try to explain what i am trying to do...
Using the KnowledgeTree Open Source DMS, i need to write an app that will allow me to upload files in bulk. I am using Visual Basic 2008 Express to try to achieve this.

From the documentation, I am told that this requires a two-step process.
"The web service exposes two sets of add_document(). 1) a two phase add_document. It requires an http post to upload the initial document followed by the web service call to add_document" - I won't mention the 2nd option since it is not suitable.

"Webservices/upload.php
This requires the following post parameters:
documentname
sessionid "
Note: the above information is apparantly wrong - the requirements are actually named "action" and "session_id". This can be checked on the above link to the php.

The first thing I do with my program is connect to the webservice via the SOAP interface and authenticate. This returns a SessionID.
I have also performed sucessfully the second part of the webservices - the add_document function which also uses the SessionID, but this can be done via http GET and is relatively easy.
The upload.php should create a temporary copy of the file and return a filename which is used by the add_document function - i have tested add_document by manually creating the temporary file and using add_document to process it.

What I cant get a grasp on is exactly what the upload.php requires to be uploaded - where exactly do I put the session_id and action(documentname)? It is the structure of HTTP POST that I dont understand. Imagine I am uploading HelloWorld.txt which contains "Hello World" - what would the entire HTTP POST look like? Just substitue my SessionID key with xxx's and i will get the idea!

Thanks for your time!
SStory

I will have to look at that tomorrow. I wonder if expects XML or something.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
Rabbit80

I have used the code below as a starting point for my HTTP POST. This accepts the session_ID and action - I just need some help in actually uploading the file. xFileName contains a path to the file i.e. "C:\test.pdf"

Help!!!

Thanks
Public Shared Function PostData(ByVal Session_ID As String, ByVal xFileName As String, ByVal URL As String, Optional ByVal NeedResponse As Boolean = True) As String
        Dim ReturnPage As String = String.Empty
        Dim Uri As New Uri(URL)
        Dim xSession
        Dim Req As HttpWebRequest
 
        Req = CType(WebRequest.Create(URL), HttpWebRequest)
        Req.Method = "Post"
        Req.ContentType = "application/x-www-form-urlencoded"
        xSession = "&session_id=" + Session_ID + "&action=C"
 
        Dim sw As New StreamWriter(Req.GetRequestStream)
        sw.Write(xSession)
        sw.Close()
        sw.Dispose()
        If NeedResponse Then
            Dim sr As New StreamReader(Req.GetResponse.GetResponseStream)
            ReturnPage = sr.ReadToEnd
            sr.Close()
            sr.Dispose()
        End If
        Req.GetResponse.Close()
        Return ReturnPage
    End Function

Open in new window

SStory

I'm currently on vacation so it will be at least Wed. before I will be taking hard looks at this questions.  Sorry.
ASKER
Rabbit80

No problem - I was away for a week which is why it got left for a while! Do you know of any good books or websites describing the format for HTTP POST? in particular sending binary data and form data in a multipart post - it is still the structure of the POST that i am struggling to understand.
Your help has saved me hundreds of hours of internet surfing.
fblack61
SStory

ASKER CERTIFIED SOLUTION
Rabbit80

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question