Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8979015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:44:48+00:00 2026-06-15T19:44:48+00:00

I am new to Java. I wrote an applet with a gui that sends

  • 0

I am new to Java. I wrote an applet with a gui that sends results (int w and int p) to a server, and I get the “411 Length Required” error. What am I doing wrong? How do you set a Content-Length?

This is the method that communicates with the server:

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());
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:44:50+00:00Added an answer on June 15, 2026 at 7:44 pm

    Request uri includes query , can use GET method.

    connection.setRequestMethod("POST"); // modify to GET
    connection.setRequestProperty("Content-Length", length); //remove the line
    

    if use POST method, include ‘content-length’ heade, must send data.

    for example:

    public void sendPoints1(int w, int p){
    
        try {
            String url = "http://somename.com:309/api/Results";
            //value type is int ,don't need URLEncoder.
            byte[] data = ("key="+w+"&value="+p).getBytes("UTF-8");
    
            HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Length", data.length);
     connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); //URLencode...
            OuptputStream out = connection.getOutputStream();
            out.write(data);
            out.flush();
            InputStream in = connnection.getInputStream();
            //read .....
            System.out.println("Responce Code:    " + connection.getResponseCode());
            System.out.println("Responce Message: " + connection.getResponseMessage());
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Java applet that streams video (MJPEG) from a server. I wrote
I wrote a simple downloader as Java applet. During some tests I discover that
Hey I'm new to java servlets and I am trying to write one that
I am starting a new Java web project that will be worked on by
I have wrote my first java applet. It basically loads some images from my
i have a java applet that calls this url that links to a php
To get more familiar with the new Java EE 6 System I have created
I am using Derby database. I wrote this query: InputStream is = new java.io.ByteArrayInputStream(BYTES);
I'm trying to write a Java applet that will display a user-inputted String, it
I'm really new to Java and I have to create an applet for signing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.