简体   繁体   中英

Java - use java.net.Socket to connect server with multiple endpoint

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM