• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 319
  • Last Modified:

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

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


1 Solution
käµfm³d 👽Commented:
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.
sglewAuthor Commented:
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!

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

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now