0

May be I am missing something very simple. But after doing multiple search I am unable to find a solution for my scenario.

I have a python web server running on my localhost with multiple endpoint. ( not written by me). I am trying to interact ( receiving and posting some data) with the server using java.

The example I have seen in Java client python server socket programming or in Send File From Python Server to Java Client using InetAddress and port number to connect the server. But I want to create separate connections for different endpoints - for example : localhost:8080/endpoint1 or localhost:8080/endpoint2

Is there any way to specify the endpoint in java.net.Socket while making the connection? Or I have to use other API ?

Example shown in http://www.programmingforliving.com/2013/08/jsr-356-java-api-for-websocket-client-api.html does use full URI to connect, but here the server code is annotated to make it work. I can not change my python server code.

Any help is hugely appreciated.

  • If you have no control of the source the webserver runs with you have no chance to alter the way establish a connection. If the server does not support what you got to live with that. – L.Spillner Jun 01 '18 at 20:42
  • @L.Spillner : Thanks for responding. But I can run the server, and check data in my browser with url : localhost:8080/endpoint1. I am looking for a way to connect from a java client. – InconsistentHashing Jun 01 '18 at 20:46
  • Ah now I got your question. What exactly happens when you check the data with your brwoser? Because currently it sounds like the server is running a [RESTful webservice](http://www.drdobbs.com/web-development/restful-web-services-a-tutorial/240169069). If that's the case you should take a look at the [HttpURLConnection](https://docs.oracle.com/javase/7/docs/api/java/net/HttpURLConnection.html) class which provides serveral ways to interact with webservices. Featureing the most common Http methods: GET, POST, DELETE, PATCH and many more. – L.Spillner Jun 01 '18 at 20:50
  • Thanks, will check that. With different endpoints I can see some data in the browser in different format ( String, XML etc). – InconsistentHashing Jun 01 '18 at 21:01
  • If you only need to read the data present on the server `HttpURLConnection` will be totaly sufficient. If you need to transmit data and the server does not support _POST_ -Rquests I am unable to help (to little know-how about that topic). – L.Spillner Jun 01 '18 at 21:03
  • 1
    Sockets don't have endpoints like URLs. Your question betrays some confusion. All you need is `new Socket("localhost", 8080)` in both cases. But then you have to speak HTTP. You should be using the `URL` class, not a `Socket`. – user207421 Jun 01 '18 at 21:20
  • @L.Spillner, thanks much for replying. HttpURLConnection worked like a charm! – InconsistentHashing Jun 03 '18 at 07:39

1 Answers1

0

Use a http client library like one from apache.

https://hc.apache.org/httpcomponents-client-4.3.x/quickstart.html

HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://server:8080/endpoint1");
HttpResponse response = client.execute(request);
response.getEntity()
...

Do not handle this using socket.

gagan singh
  • 1,591
  • 1
  • 7
  • 12