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.

7
  • 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. Commented Jun 1, 2018 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. Commented Jun 1, 2018 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. If that's the case you should take a look at the HttpURLConnection class which provides serveral ways to interact with webservices. Featureing the most common Http methods: GET, POST, DELETE, PATCH and many more. Commented Jun 1, 2018 at 20:50
  • Thanks, will check that. With different endpoints I can see some data in the browser in different format ( String, XML etc). Commented Jun 1, 2018 at 21:01
  • 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. Commented Jun 1, 2018 at 21:20

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.