Link to home
Start Free TrialLog in
Avatar of suresh pondicherry
suresh pondicherryFlag for United States of America

asked on

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
Avatar of kaufmed
kaufmed
Flag of United States of America image

If your service is hosted using port 8080, why wouldn't you be passing the port number?
Avatar of suresh pondicherry

ASKER

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
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

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.
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


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
ASKER CERTIFIED SOLUTION
Avatar of suresh pondicherry
suresh pondicherry
Flag of United States of America 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
Solution is to establish the connection through handler.