0

I want to build a protocol on top of http (like WebDAV). So it makes sense to use HttpURLConnection because it contains all basic methods, response codes and so on. Unfortunately it prevents setting other methods like:

HttpURLConnection con = URL.openConnection( "http://abc.def/someServlet" );
con.setRequestMethod( "MyMethod" );

throws a ProtocolException Invalid method MyMethod.

So I extended it and overwrote "setRequestMethod( String )" to accept "MyMethod". This does not work, because URL only returns a HttpURLConnection. Is there any way to use HttpURLConnection to accept other methods?

Jemolah
  • 1,962
  • 3
  • 24
  • 41

3 Answers3

0

You might wish to consider RESTful interface. You still use http werbs but you are level up, don't see it, can do any interface, use factories, like:

public String readFileAsString(String url) {
    String result = null;
    try {
        Client client = Client.create();
        WebResource webResource = client.resource(url);
        ClientResponse response = webResource.type("text/plain").get(ClientResponse.class);
        result= response.getEntity(String.class);
        response.close();
    } catch (Exception e) {
        e.printStackTrace();
        result = "";
    }

    return result;
}
Milan Novkovic
  • 126
  • 1
  • 2
  • 11
  • How can that help? – Jemolah Mar 13 '17 at 20:33
  • You go level up, simpler app programming, can use factories, do any service, in fact, no http hastle... Jersey Example: public String readFileAsString(String url) { String result = null; try { Client client = Client.create(); WebResource webResource = client.resource(url); ClientResponse response = webResource.type("text/plain").get(ClientResponse.class); result= response.getEntity(String.class); response.close(); } catch (Exception e) { e.printStackTrace(); result = ""; } return result; } – Milan Novkovic Mar 13 '17 at 20:47
  • I don't see how this can solve my problem. I have to perform some complex operations which are not covered by get or post. So how can I extend HttpURLConnection with additional methods without rewriting the complete URL class? – Jemolah Mar 13 '17 at 20:56
  • It's hard to say without knowing the protocol. Maybe this is not good for you. REST and WebSockets do all the protocol requirements I have but they are, quite possibly, simple. We even get neat real-time between any mix of Java, C++ and JavaScript, although I am an app programmer and not that versed in lower level. – Milan Novkovic Mar 13 '17 at 21:03
  • We did have a lot of old HttpURLConnection code, and an odd issue now and then. All disappeared as we gave up on HttpURLConnection, and I cannot recall now what they were. – Milan Novkovic Mar 13 '17 at 21:08
  • what else do you use now instead of HttpURLConnection? – Jemolah Mar 13 '17 at 21:20
  • Jersey (REST) does use HttpURLConnection, I guess, but hides it and the problems from me, while WebSockes are for real-time and complex handshaking – Milan Novkovic Mar 13 '17 at 21:23
  • Seems like this problem is a lack in the JAVA API. It explicitly prevents adding methods on top of the basic http methods.I will look for a Java lib providing WebDAV and try to find out, how they solved that problem. Thx for suggestions – Jemolah Mar 13 '17 at 21:49
0

Please don't, I tell you this because I did the same error when I started programming in java and it did not pay off.

Ask yourself this question, will you ever pass the child class to a function / method / constructor which accepts the parent class?

For example will you put in a collection a mix of HttpURLConnection and MyServiceURLConnection.

If not so, you will be better off with a brand new class which uses a HttpURLConnection to provide the low level operations.

minus
  • 2,646
  • 15
  • 18
  • Good point. But how can I do that without rewriting java.net.URL or at least providing the whole JVM application with a modified http-handler? – Jemolah Mar 13 '17 at 21:00
  • You should provide some skeleton code if you want help with that. Read this answer if you are interested in creating a new protocol : http://stackoverflow.com/questions/26363573/registering-and-using-a-custom-java-net-url-protocol – minus Mar 13 '17 at 21:08
  • 1
    The skeleton code is the one in my question. I do not want to create a new protocol. I only need additional methods in http requests. – Jemolah Mar 13 '17 at 21:21
0

url dosent return HTTPURLConnection , it returns URLConnection because you didnt cast it so you cannot override the HTTPURlConnection method setRequestMethod()

Sherif Eldeeb
  • 199
  • 2
  • 11