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