简体   繁体   English

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

[英]java.net.UnknownHostException: http://localhost:8082/consume/create

I am trying to make a http post to the server and I am getting a java.net.UnknownHostException at this line of the code 我正在尝试向服务器发布http帖子,并且在此行代码中获取java.net.UnknownHostException

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 wrong? 请问我哪里错了?

Instead of using Socket class, you should use URL class. 不要使用Socket类,而应该使用URL类。 Socket requires a host name like localhost. 套接字需要一个主机名,例如localhost。 It does not understand URL 它不了解网址

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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