简体   繁体   中英

Java HTTP POST request (JSON) to PHP server

I have an application (Java) that needs to send json to a php web service.

This is my method to send User in JSON :

public void login(User user) throws IOException {
    Gson gson = new Gson();
     String json = gson.toJson(user);
     System.out.println(json);
      String url = "http://localhost/testserveur/index.php";
     URL obj = new URL(url);
     HttpURLConnection con = (HttpURLConnection)obj.openConnection();

     con.setRequestMethod("POST");
     con.setRequestProperty("json", json);

     con.setDoOutput(true);
     try (DataOutputStream wr = new DataOutputStream(con.getOutputStream())) {
         wr.flush();
     }

     int responseCode = con.getResponseCode();
     System.out.println(responseCode);

 }

And my php code :

$string=$_POST['json'];

I tried to insert in my database but $_POST['json'] does not exist.

I didn't see you to posting anything. Add this to your code:

String param = "json=" + URLEncoder.encode(json, "UTF-8");
wr.write(param.getBytes());

This is not right:

con.setRequestProperty("json", json);

setRequestProperty is not used to set the HTTP payload. It is used to set the HTTP headers. For example, you should set the content type accordingly anyway. Like this:

con.setContentType("application/json");

The actual data that you are going to post goes into the HTTP body. You just write it to the end of the stream (before flush):

Here it depends on your implementation on the web server if the data needs to be escaped. If you read the body of the post and interpret it as JSON straight away, it does not need to be escaped:

wr.write(json);

If you transmit one or more JSON strings through parameters (which it looks like, since you are parsing it on the server like $_POST['json']), then you need to url-escape the string:

wr.write("json=" + URLEncoder.encode(json, "UTF-8"));

Im not very familiar with php. You might need to url-decode the string on the server before processing the received json-string any further.

Thx for your help.

This works :

public void login(User user) throws IOException {

  Gson gson = new Gson(); String json = gson.toJson(user); System.out.println(json); String url = "http://localhost/testserveur/index.php"; URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setDoOutput(true); con.setRequestMethod("POST"); con.setRequestProperty("json", json); OutputStream os = con.getOutputStream(); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); //wr.write(new String("json=" + json).getBytes()); String param = "json=" + URLEncoder.encode(json, "UTF-8"); wr.write(param.getBytes()); wr.flush(); wr.close(); int responseCode = con.getResponseCode(); System.out.println(responseCode); } 

PHP :

$string=$_POST['json'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM