Results 1 to 4 of 4
  1. #1
    psdev is offline Member
    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Exclamation Please Help: Java Applet sending results to server doesnt work

    I am new to Java. I wrote an applet that sends results (int w and int p) to a server, and I get the "411 Length Required" error. What am I doing wrong?

    This is the method that communicates with the server:

    Java Code:
    public void sendPoints1(int w, int p){
    
            try {
                String url = "http://somename.com:309/api/Results";
                String charset = "UTF-8";
                String query = String.format("?key=%s&value=%s",
                    URLEncoder.encode(String.valueOf(w), charset),
                    URLEncoder.encode(String.valueOf(p), charset));
                String length = String.valueOf((url + query).getBytes("UTF-8").length);
                
                HttpURLConnection connection = (HttpURLConnection) new URL(url + query).openConnection();
                connection.setRequestMethod("POST");
                connection.setRequestProperty("Content-Length", length);
                connection.connect();
                System.out.println("Responce Code:    " + connection.getResponseCode());
                System.out.println("Responce Message: " + connection.getResponseMessage());
            } catch (Exception e) {
                System.err.println(e.getMessage());
            }
           
        }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default Re: Please Help: Java Applet sending results to server doesnt work

    I might be being blind (it is Friday after all), but where are you putting 'w' and 'p' into your URL?
    I see you encode them, and then do nothing with the encoded string. The encode() method returns the encoded String, since Strings are immutable neither 'w' nor 'p' are changed. But you do nothing with the results.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    psdev is offline Member
    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Default Re: Please Help: Java Applet sending results to server doesnt work

    The 'w' and 'p' are being put in instead of the %s in the String query. Is the Content-Length right?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default Re: Please Help: Java Applet sending results to server doesnt work

    I don't think you understood me.
    Nowhere in that code that I can see are you actually substituting 'w' and 'p' into your query string.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Two classes: can someone tell me why this doesnt work?
    By katiebear128 in forum New To Java
    Replies: 12
    Last Post: 11-08-2011, 03:08 AM
  2. Replies: 0
    Last Post: 09-20-2011, 11:58 PM
  3. Java Applet different results in IDE and browser
    By edo_nick in forum Java Applets
    Replies: 2
    Last Post: 03-15-2011, 01:24 PM
  4. Applet doesnt start or it doesnt show
    By 3dprogger in forum Java Applets
    Replies: 2
    Last Post: 01-07-2011, 07:03 PM
  5. java doesnt allow vista to work
    By 10rosas in forum New To Java
    Replies: 5
    Last Post: 12-22-2008, 04:23 PM

Tags for this Thread

Posting Permissions

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