Results 1 to 18 of 18
  1. #1
    Dude is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

    Default HTTP POST Request (HttpURLConnection)

    I'm trying to program a channel changer for my Dreambox with java. The Dreambox is a tv streamer that also have a web interface. You can change channel by entering:

    "http://192.168.0.66/cgi-bin/zapTo?path=(aChannelNumber)"

    in any web browser.

    This is the behaviour im trying to duplicate but without a browser and in a java application. I've figured out that I probably need to use HttpURLConnection to do this and this is what I got so far:
    PHP Code:
    URL url = new URL("http://192.168.0.66/cgi-bin/zapTo?path=");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("POST");
    conn.setDoOutput(true);
                
    BufferedWriter out = new BufferedWriter( new OutputStreamWriter(conn.getOutputStream()));
    out.write("aChannelNumber");
    out.flush();
    out.close();
    This is not working at all and I hope someone knows how to achieve this.
    Last edited by Dude; 07-12-2011 at 11:05 PM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    You don't need to write to the output stream if the URL itself contains the query strings (in other words, this uses a GET protocol rather than a PUT protocol). Just append your channel number to the URL

  3. #3
    Dude is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by doWhile View Post
    You don't need to write to the output stream if the URL itself contains the query strings (in other words, this uses a GET protocol rather than a PUT protocol). Just append your channel number to the URL
    So you mean all I have to write is:
    PHP Code:
        URL url = new URL("http://192.168.0.66/cgi-bin/zapTo?path=aChannelNumber");
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");
    But that still doesn't work.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,570
    Rep Power
    25

    Default

    I think you need to do something to cause the GET to be sent.
    Try adding:
    Object obj = conn.getContent();

  5. #5
    Dude is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    I think you need to do something to cause the GET to be sent.
    Try adding:
    Object obj = conn.getContent();
    This made something happen...

    Eclipse gave me the 401 HTTP response code which is kinda weird because I am authorized..

    I even tried "http://username:password@192.168.0.66/cgi-bin/zapTo?path=aChannelNumber" and still no solution.

    But I think we're getting there.

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    What is channelnumber, don't you need to specify an actual value? While I don't fully understand how your setup works, if the url is indeed as you describe, replace channel number with an actual value.

    Java Code:
    String channel = "20";//specifies channel 20
    String urlString = "http://192.168.0.66/cgi-bin/zapTo?path=(" + channel + ")";
    URL url = new URL(urlString);

  7. #7
    Dude is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

    Default

    The channels comes in the form of 1:0:Mode:SID:TransponderID:NetworkID:Namespace:0:0 :0: (example 1:0:1:41e:22:a027:ffff0000:0:0:0:).

    PHP Code:
    URL url = new URL("http://192.168.0.66/cgi-bin/zapTo?path=1:0:1:51d:2f:a027:ffff0000:0:0:0:");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    Object obj = conn.getContent();
    So if I want to change to the example channel via a browser I write "http://192.168.0.66/cgi-bin/zapTo?path=1:0:1:51d:2f:a027:ffff0000:0:0:0:" in a browser and press enter.

    But the problem is making the java application perform this.
    Last edited by Dude; 07-13-2011 at 12:19 AM.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,570
    Rep Power
    25

    Default

    What happens when you type that URL string into a browser? Is there any more user action required after the initial response from the server?

  9. #9
    Dude is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    What happens when you type that URL string into a browser? Is there any more user action required after the initial response from the server?
    Nope - it just switches channel to stream. It doesn't even load a page. So that's why I figured it should be easy to make an application that simulates this input.
    Last edited by Dude; 07-13-2011 at 12:58 AM.

  10. #10
    Dude is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

    Default

    I'm still stuck on receiving the HTTP response code 401 with the following code:

    PHP Code:
    			
    URL url = new URL("http://user:password@192.168.0.66/cgi-bin/zapTo?path=1:0:1:51a:2f:a027:ffff0000:0:0:0:");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    Object obj = conn.getContent();
    When entering "http://user:password@192.168.0.66/cgi-bin/zapTo?path=1:0:1:51a:2f:a027:ffff0000:0:0:0:" in a browser it works fine without any error code. So I guess there is something in Eclipse or in Java that's blocking this connection.

    Anyone know what it might be?

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,570
    Rep Power
    25

    Default

    Have you looked up what status code 401 means?
    Can you look at what your browser sends to the site and then try to replicate that with your program?

  12. #12
    Dude is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

    Default

    Status code 401 means unauthorized - which is not true.

    Well the browser just sends a query to the server and I should be replicating it with my URL string in the code above. But somehow I'm being blocked by the server when trying to connect via HttpURLConnection.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,570
    Rep Power
    25

    Default

    Status code 401 means unauthorized - which is not true.
    Are you sure? The server thinks so.
    the browser just sends a query to the server
    Have you looked at the Request Header that the browser is sending? There are records there that you might not be setting with your code.
    The server sees the Request Header you send and does NOT like it.

  14. #14
    Dude is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Are you sure? The server thinks so.

    Have you looked at the Request Header that the browser is sending? There are records there that you might not be setting with your code.
    The server sees the Request Header you send and does NOT like it.
    I'm afraid I'm not quite sure what you're getting at.

    Do you mean I've missed some kind of url encoding?

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,570
    Rep Power
    25

    Default

    You need to do some research about the HTTP protocol. There are header records sent from the browser to the server and header records sent from the server to the browser.

  16. #16
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    Some servers require the proper header's to be sent. Unless you know what your browser is sending in the header its tough to say, but you can start by setting the User-Agent header to something like mozilla/firefox using the setRequestProperty method. Don't know off the top of my head the specifics, but you can google to figure out the details.

  17. #17
    Dude is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

    Default

    Thanks for the tips everyone - I think I get it now. By installing Live Http Headers add-on to firefox I managed to collect the neccesary data needed to make myself authorized through my program.

    However, I have another problem. My program throws "java.net.UnknownServiceException: no content-type" even though I've added the correct content type my browser is sending to my server.

    Any hints to the final solution?

  18. #18
    Dude is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

    Default

    Turns out my server don't return any Content-Type, so by just creating an object returning just anything my server returns I get no errors and my program works the way it should.

Similar Threads

  1. POST request using HttpUrlConnection
    By turk in forum Networking
    Replies: 3
    Last Post: 07-06-2011, 04:36 PM
  2. POST request using HttpUrlConnection
    By turk in forum Networking
    Replies: 2
    Last Post: 07-06-2011, 04:29 PM
  3. HTTP Post Request from Java
    By Darrarski in forum New To Java
    Replies: 10
    Last Post: 06-05-2010, 05:22 PM
  4. Replies: 4
    Last Post: 10-15-2008, 01:59 PM
  5. First post as per request
    By happyknappy in forum Introductions
    Replies: 3
    Last Post: 07-30-2008, 01:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •