We help IT Professionals succeed at work.

Calling REST service from VB.NET  using GET and POST method

Hi All,
I have to call Java rest services from VB.NET application. I could call the service successfully if use the port 8080. Without the port number when i call the actual URL, getting
{"The remote server returned an error: (401) Unauthorized."}
'e.Result' threw an exception of type 'System.Reflection.TargetInvocationException'

Kind regards,
Pooja
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
If your service is hosted using port 8080, why wouldn't you be passing the port number?

Author

Commented:
Hi Kaufmed,
Consider this is Java Rest url (http://devName/stored/status). I have to post the xml file in body of the url.
Earlier i could do use Get method and pass xml data using POST method using the url (http://devName:8080/stored/status).

This is a working code which uses port 8080,
 Dim pwd As String = String.Empty
        Dim reqURL As String = String.Empty
        reqURL = "http://devName:8080/stored/status"
        WebRequest.RegisterPrefix(reqURL, WebRequestCreator.ClientHttp)
        wc = New WebClient()
        pwd = String.Format("{0}:{1}", username, password)
        Dim authbytes As [Byte]() = Encoding.UTF8.GetBytes(pwd.ToCharArray())
        wc.Headers(HttpRequestHeader.Authorization) = "Basic " & Convert.ToBase64String(authbytes)
        AddHandler wc.DownloadStringCompleted, AddressOf wc_DownloadStringCompleted
        wc.DownloadStringAsync(New Uri(reqURL))

Kind regards,
Pooja
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
In my understanding, the concept of REST restrict you to one single port number. A RESTful service is one that takes advantage of existing HTTP verbs (and ones you add) to transfer data. The server name and port number are just a way of identifying where the service resides. If you are hosting your service on port 8080, then you need to access your service via port 8080. If you want to access your service with no port number, then you need to configure your service to run on port 80--the standard HTTP port.

You can certainly use POST to transfer data to your service. You need to tell this to your WebClient, though. For example:

Dim wc As New WebClient()
Dim xmlData() As Byte = System.IO.File.ReadAllBytes(@"C:\path\to\xml\file.xml")

wc.Credentials = new System.Net.NetworkCredential(username, password)

wc.UploadData("http://devName:8080/stored/status", "POST", xmlData)

Open in new window

CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
the concept of REST restrict you to one single port number.
Correction:  the concept of REST doesn't restrict you to one single port number.

Author

Commented:
Hi Kaufmed,
Thanks for the reply. First thing is i don't need to use any port numbers. Second, i could not get method name UploadData.
What i heard from the person who published the service is to call google.com  using GET method. If that works ,i have to apply the same code  to call the java rest service..
Also i am using xml string here , not a file.  I really appreciate if i got helped. Thanks for the support and eagerly expecting some tips since i am searching the answer for last 2 weeks..

Kind regards,
Pooja


Author

Commented:
Hi,
Typing mistake...
call google.com or cnn.com or yahoo.com, etc... and try to get their html (that's make sure my code will work on calling java service). is this make sense?

Kind regards,
Pooja
Hi,
I got answer for my question. I am closing this question.

Kind regards,
Pooja

Author

Commented:
Solution is to establish the connection through handler.