I try to send 2 string from my java client to my java server using Jersey, the client and the server respond correctly (code 200),
Server Code
@POST
@Path("eliminar")
@Consumes({ MediaType.MULTIPART_FORM_DATA, ("text/plain") })
@Produces(MediaType.APPLICATION_JSON)
public String eliminar(
@FormDataParam("primero") String primero,
@FormDataParam("segundo") String segundo){
System.out.println("recibiendo primero "+primero+ " segundo "+segundo);
return "Rastalovely";
}
Client Code
package eliminar;
import java.io.*;
import java.net.*;
import java.util.*;
public class Limpio {
final static String URL = "http://localhost:8080/restdemo/jaxrs/customers/eliminar";
public static void main(String[] args) {
String crlf = "\r\n";
String twoHyphens = "--";
try {
String boundary = "--" + Long.toString(System.currentTimeMillis()) + "--";
String parametros="primero=primero&segundo=segundo";
byte[] postDataBytes = parametros.toString().getBytes();
URL url = new URL(URL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
DataOutputStream request = new DataOutputStream(connection.getOutputStream());
request.writeBytes(twoHyphens + boundary + crlf);
request.writeBytes(parametros);
System.out.println("responseCode " + connection.getResponseCode());
//Aqui java retorna
BufferedReader in =new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuffer sb=new StringBuffer();
String line;
while((line=in.readLine())!=null){
sb.append(line);
}
in.close();
System.out.println("salida "+sb.toString());
} catch (Exception e) {
System.out.println("error " + e.getMessage());
}
}
}
The results: When the client runs, I get the variables on the server console as null and on the client side if he receives the message from the server.
So I do not know if the way I send the variables is correct.