hamed
hamed

Reputation: 8033

java - do json remote procedure call(RPC) from client

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.

Upvotes: 0

Views: 1654

Answers (2)

Daisy Li
Daisy Li

Reputation: 54

i think the reason is that the 'remote_method_name' you provided is wrong. since you are making a http call, here is a brief example for you to refer. for the page you are reading now, the request should be:

curl -v 'http://stackoverflow.com/questions/40171522/java-do-json-remote-procedure-callrpc-from-client'
*   Trying 151.101.193.69...
* Connected to stackoverflow.com (151.101.193.69) port 80 (#0)
> GET /questions/40171522/java-do-json-remote-procedure-callrpc-from-client HTTP/1.1
> Host: stackoverflow.com
> User-Agent: curl/7.43.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< Cache-Control: private
< Content-Type: text/html; charset=utf-8
< Last-Modified: Fri, 21 Oct 2016 09:01:29 GMT
< X-Frame-Options: SAMEORIGIN
< X-Request-Guid: 405a2900-543b-4a97-8c62-8fa9019ab934
< Content-Length: 77809
< Accept-Ranges: bytes
< Date: Fri, 21 Oct 2016 09:18:59 GMT
< Via: 1.1 varnish
< Age: 0
< Connection: keep-alive
< X-Served-By: cache-ams4437-AMS
< X-Cache: MISS
< X-Cache-Hits: 0
< X-Timer: S1477041539.483029,VS0,VE95
< X-DNS-Prefetch-Control: off
< Set-Cookie: prov=aef7ece4-db49-60e0-3209-a2a2830d8749;         domain=.stackoverflow.com; expires=Fri, 01-Jan-2055 00:00:00 GMT; path=/; HttpOnly
< 
<!DOCTYPE html>
<html itemscope itemtype="http://schema.org/QAPage">
......

Upvotes: 0

Joeri Hendrickx
Joeri Hendrickx

Reputation: 17445

First of all, even though you say you want to use raw tcp sockets, you're very clearly trying to make an HTTP rest request. It will be way easier and more appropriate to use an http client for that. I you don't want to use third-party libraries, use the built-in HttpUrlConnection (example usage).

Another advantage is that using an http client will give you a clear(er) error message.

Second, are you sure about that content-type? If you're trying to submit json, normally the header to set would be Content-Type: application/json.

Third, if you're getting 404 not found, I'd bet the url you're posting to is incorrect. Double-check the domain and baseurl with whoever gave you the specs for this API. Right now your URL is essentially http://remote_ip:remote_port/remote_method_name which is quite unlikely to be correct.

Upvotes: 1

Related Questions