Link to home
Start Free TrialLog in
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?
Avatar of SStory
SStory
Flag of United States of America image

Avatar of Rabbit80

ASKER

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?
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 
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?
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!
I will have to look at that tomorrow. I wonder if expects XML or something.
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

I'm currently on vacation so it will be at least Wed. before I will be taking hard looks at this questions.  Sorry.
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.
ASKER CERTIFIED SOLUTION
Avatar of Rabbit80
Rabbit80
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial