9 Replies Latest reply on Jun 18, 2011 1:32 AM by aidreamer

    Alternative to (Deprecated)HttpRequest

      Now that most of the javafx.io.http package is Deprecated what is the alternative.
        • 1. Re: Alternative to (Deprecated)HttpRequest
          I think that that package is deprecated because it depends on Task class that is deprecated too. I did not found any alternative but as i heard Task class works but it will be changed in the future so that is probably the main reason why it is deprecated.
          If am wrong feel free to correct me.
          • 2. Re: Alternative to (Deprecated)HttpRequest
            Java REST API would be a good alternative.
            • 3. Re: Alternative to (Deprecated)HttpRequest
               * Use this to be certain string data is sent in ascii format.
              static final Charset ASCII_CHARSET = Charset.forName("US-ASCII");
              try {
                   * Create the URL object.
                  URL url = new URL("http://www.somwebsite.com/thepage.html?some_key=some_value");
                  //Create the connection object from the url object.
                  URLConnection connection = url.openConnection();
                  //Since this is an Http URL, we can cast the URLConnection to something more specific.
                  HttpURLConnection httpConnection = (HttpURLConnection) connection;
                  //Tell it we are reading and writing.
                  //We are sending post headers, so set this appropriately.
                  //Tell it the MIME format of the request.
                  httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                  //Send the post headers.
                  OutputStream outputStream = httpConnection.getOutputStream();
                  //Recieve whatever data here.
                  InputStream inputStream = httpConnection.getInputStream();
              } catch (IOException ex) { //Something went wrong. Tell us what.
              I was a little distracted when writing this, so there may be errors. If you want to send large files, it gets much more complicated. Had to build my own class for that.

              Edited by: aidreamer on Jun 17, 2011 6:38 PM
              • 4. Re: Alternative to (Deprecated)HttpRequest
                I used the dispatcher
                private Service service;
                    private JAXBContext jc;
                    private static final String url = "http://www.something.com/";
                    private static final QName qname = new QName("", "");
                    public RESTclient() {
                        try {
                            jc = JAXBContext.newInstance("com.something.models");
                        } catch (JAXBException je) {
                            System.out.println("Cannot create JAXBContext " + je);
                    public Product[] fetchServerProduct() {
                        service = Service.create(qname);
                        service.addPort(qname, HTTPBinding.HTTP_BINDING, url + "getProduct");
                        Dispatch<Source> dispatcher = service.createDispatch(qname, Source.class, Service.Mode.MESSAGE);
                        Map<String, Object> requestContext = dispatcher.getRequestContext();
                        requestContext.put(MessageContext.HTTP_REQUEST_METHOD, "GET");
                        StreamSource result = (StreamSource) dispatcher.invoke(null);
                        return parseXML(result);
                • 5. Re: Alternative to (Deprecated)HttpRequest
                  Richard Bair-Oracle
                  The best thing I've found as a replacement are the Jersey client APIs (http://blogs.oracle.com/enterprisetechtips/entry/consuming_restful_web_services_with). There is an expert group working on adapting these to be part of the Java standard for Java 8. Because we didn't want to define completely different or competing APIs to what is hopefully going to be part of the standard, we decided to pull out HttpRequest and work with this expert group instead.

                  I will post some blog at http://fxexperience.com when I've completed the refactoring for Task such that I can demonstrate how to use the Jersey Client APIs for REST based web services, using the (almost done) Task / Service APIs.
                  • 6. Re: Alternative to (Deprecated)HttpRequest
                    For some stupid reason
                    The 'stupid reason' is that the URL is specified as a String, which isn't necessarily a string literal, isn't necessarily typed in at development time, may have been retrieved from a database or constructed at runtime or entered by the user, and may therefore be malformed at runtime.
                    • 7. Re: Alternative to (Deprecated)HttpRequest
                      Ok. Kind of makes me wonder why Integer.parseInt lets catching NumberFormatException be optional since that is at least as likely to be user input.
                      • 8. Re: Alternative to (Deprecated)HttpRequest
                        NumberFormatException is a RuntimeException.
                        • 9. Re: Alternative to (Deprecated)HttpRequest
                          Huh. Learn something new everyday. I was wondering what made some exceptions optional.