I'm working on a java application. I need to call a remote api method. Suppose I have this information: remote_ip
, remote_port
, remote_method_name
and some key-value
data to post. I need to post my data to remote server through TCP protocol. I tested Sockets
in this way, but not working:
Socket socket = new Socket(remote_ip, remote_port);
BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8"));
String params = URLEncoder.encode("key1", "UTF-8")
+ "=" + URLEncoder.encode(value1, "UTF-8");
params += "&" + URLEncoder.encode("key2", "UTF-8")
+ "=" + URLEncoder.encode(value2, "UTF-8");
wr.write("POST " + remote_method_name + " HTTP/1.0\r\n");
wr.write("Content-Length: " + params.length() + "\r\n");
wr.write("Content-Type: application/x-www-form-urlencoded\r\n");
wr.write("\r\n");
wr.write(params);
wr.flush();
Could any one tell me how can I call api method in the correct way?
I want to do it without any third-party library if possible.
Any help would be gratefully appreciated.