14284

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

Question:

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

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?

Answer1:

Instead of using Socket class, you should use URL class. Socket requires a host name like localhost. It does not understand URL

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

Recommend

  • sum of random numbers using angularjs promises
  • What replaces WCF in .Net Core?
  • Can Spring MVC strictly map query strings to request parameters?
  • spring controller json receive json List
  • No mapping found using Spring3 MVC + Maven2 on GAE
  • Spring Long Polling with DeferredResult
  • ExtJS 4 Spring 3 file upload. Server sends bad response content type
  • Spring Security: @PreAuthorize works only together with @RequestMapping
  • Operation not supported on read-only collection
  • Retrofit 2 error: NetworkOnMainThreadException
  • Spring MVC @RequestParam a list of objects
  • Send Tor NEWNYM request using Java
  • 302 Redirect from http to https in Android using Dropbox short Hyperlinks
  • ORA-12154: TNS:could not resolve the connect identifier specified
  • How to escape colon (:) character while executing native SQL queries against an Informix database us
  • @Autowired for @ModelAttribute
  • Runtime error in UVA Online Judge [closed]
  • Understanding Intl.DateTimeFormat as a JavaScript object
  • Rails redirect_to another controller method throwing “Template missing”
  • NativeQuery Spring Data return object
  • Is it mandatory to have a doGet or doPost method?
  • Authentication failed with Azure Active Directory in Windows Phone
  • Creating Java object from class name with constructor, which contains parameters [duplicate]
  • How to use RequestBodyAdvice
  • Array.prototype.includes - not transformed with babel
  • Trying to switch camera back to front but getting exception
  • Free memory of cv::Mat loaded using FileStorage API
  • Suggestions to manage Login/Logout transitions
  • JTable with a ScrollPane misbehaving
  • Angular 2 constructor injection vs direct access
  • Memory offsets in inline assembly
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • Turn off referential integrity in Derby? is it possible?
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • Programmatically clearing map cache
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize