tom
tom

Reputation: 5001

Calling java servlet from c#

Is this possible, and if so can someone point me in the right direction.

Thanks

Upvotes: 3

Views: 6915

Answers (4)

P.Brian.Mackey
P.Brian.Mackey

Reputation: 44285

A couple years ago I did exactly that. You can just do a http post to the URL where the servlet is and even pass query params. In my case it was something like

http://myservlet.com?dbItem1=ipAddress&dbItem2=trackingInfo

Then the servlet can act as a DB backend or whatever. I had the servlet spit out XML which is pretty much ready to read in C#. The response can be made with HttpWebRequest and read with HttpWebResponse.

Upvotes: 0

Klaus Byskov Pedersen
Klaus Byskov Pedersen

Reputation: 120997

Given the definition of a Java Servlet:

A Servlet is a Java class in Java EE that conforms to the Java Servlet API, a protocol by which a Java class may respond to HTTP requests. They are not tied to a specific client-server protocol, but are most often used with this protocol. The word "Servlet" is often used in the meaning of "HTTP Servlet".

The correct way would be to "call" the servlet using an http request. In .net you can use the HttpWebRequest class for this purpose.

Upvotes: 2

Sankar Ganesh PMP
Sankar Ganesh PMP

Reputation: 12027

You can use HttpWebRequest as shown below,

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://sanserver:8080/IoSystem/ToAdd?CheckLetter=SAN");

// Execute the request

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

Upvotes: 2

Darin Dimitrov
Darin Dimitrov

Reputation: 1039080

You need to explain what do you mean by calling but you could definetely use a web client to send an HTTP request to a remote url and fetch the result:

using (var client = new WebClient())
{
    string result = client.DownloadString("http://example.com/yoursevletaddress");
    // TODO: do something with the returned content from the servlet
}

Upvotes: 4

Related Questions