开发者

Calling java servlet from c#

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

开发者_运维技巧Thanks


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
}


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();


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜