Java.net.UnknownHostException: http: // localhost: 8082 / consume / create

I try to make an http message on the server and I get java.net.UnknownHostException in this line of code

Socket socket = new Socket(REST_SERVICE_URI, 8082);

this is the controller that receives the request

@RequestMapping(value="AddService",method = RequestMethod.POST)
@ResponseBody
 public void addService(@ModelAttribute("servDetForm") xxxx tb) throws IOException{
    //return dataServices.addService(tb);

     Socket socket = new Socket(REST_SERVICE_URI, 8082);
     String request = "GET / HTTP/1.0\r\n\r\n";
     OutputStream os = socket.getOutputStream();
     os.write(request.getBytes());
     os.flush();

     InputStream is = socket.getInputStream();
     int ch;
     while( (ch=is.read())!= -1)
         System.out.print((char)ch);
     socket.close(); 
 }

Please, where is my mistake?

0
source share
1 answer

Instead of using the Socket class, you should use the URL class. Socket requires a host name, for example localhost. He does not understand the URL

URL url = new URL(REST_SERVICE_URI);
Object content = url.getContent();
0
source

All Articles