How to post to a REST Service From my VB.NET 2008 Windows Forms Application

Posted on 2012-09-05
Last Modified: 2013-01-22
Hi All,

I am a newbiew to REST services.

I was given a url by an entity (government) and told I could use a "REST web transfer" to transfer data to them.

I was given a schema in XSD format and the url.  This is all of the information I have been provided.  They indicate that this is all the information they have.

I do not know if the REST service I am calling uses POST or GET.

I do know that when I attemp to contact it, I get

    The remote server returned an error: (404) Not found.

Is there a way to query the service to get more information about the REST service and verifying where it is and what it expects?  I believe this might be called the service "contract"?

I have attempted to send the file via POST, but get the error above.

this is my source code:

    Private Sub TestFileUpload()

        Dim strPostURL As String = "SomeURL"

        Dim requestStream As Stream = Nothing
        Dim fileStream As FileStream = Nothing
        Dim uploadResponse As Net.HttpWebResponse = Nothing


            Dim uploadRequest As Net.HttpWebRequest = CType(Net.HttpWebRequest.Create(strPostURL), Net.HttpWebRequest)
            uploadRequest.Method = Net.WebRequestMethods.Http.Post
            ' UploadFile is not supported through an Http proxy
            ' so we disable the proxy for this request.
            uploadRequest.Proxy = Nothing

            requestStream = uploadRequest.GetRequestStream()
            fileStream = File.Open("c:\MyFile.xml", FileMode.Open)

            Dim buffer(1024) As Byte
            Dim bytesRead As Integer
            While True
                bytesRead = fileStream.Read(buffer, 0, buffer.Length)
                If bytesRead = 0 Then
                    Exit While
                End If
                requestStream.Write(buffer, 0, bytesRead)
            End While

            ' The request stream must be closed before getting the response.
            If Not IsNothing(fileStream) Then
                fileStream = Nothing
            End If

            uploadResponse = uploadRequest.GetResponse()
            Dim responseReader As StreamReader = New StreamReader(uploadRequest.GetResponse.GetResponseStream())
            Dim x As String = responseReader.ReadToEnd()
            x = "XML RESPONSE:" & x

        Catch ex As UriFormatException
            MsgBox("Error 1:" & ex.Message)
        Catch ex As IOException
            MsgBox("Error 2:" & ex.Message)
        Catch ex As Net.WebException
            MsgBox("Error 3:" & ex.Message)
            If Not IsNothing(fileStream) Then
                fileStream = Nothing
            End If
            If uploadResponse IsNot Nothing Then
            End If
            If fileStream IsNot Nothing Then
            End If
            If requestStream IsNot Nothing Then
            End If
        End Try

    End Sub


Question by:sglew
    LVL 74

    Accepted Solution

    Are you certain the URL is correct? A "Not Found" usually means you are requesting a resource which does not exist.

    Many REST frameworks provide help pages which describe the endpoint and its expected parameters and available verbs. You would probably need to know what language the entity wrote the service in in order to track down that page--unless they can provide you with it.

    Author Comment

    Thanks so much for your response!

    I asked the person at the government entity (I am trying to stay discreet) to make sure that the URL was good, notified him/her that I was getting the not found error, and asked an indirect question "Is it possible that credentials may be required" in order to prod them to perform a little more research.

    Knowing that I would get the stock answer that I got, I opened this question in the hopes that I could get around the obstacle another way.

    Thanks again for your response!

    LVL 53

    Expert Comment

    This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.

    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
    If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now