直接上代码吧,和前面几篇文章都差不多

java代码:

package PostPager;


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class PostXml {



    public static void main(String args[]) {
        try {
            JSONObject  obj = new JSONObject();
                obj.append("app_name", "全民大讨论");
                obj.append("app_ip", "10.21.243.234");
                obj.append("app_port", 8080);
                obj.append("app_type", "001");
                obj.append("app_area", "asd");

            CloseableHttpClient httpclient = HttpClients.createDefault();
            System.out.println(obj);

            HttpPost httpPost = new HttpPost("http://119.29.85.118//test.php");
            httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");

            // 解决中文乱码问题
            StringEntity stringEntity = new StringEntity(obj.toString(), "UTF-8");
            stringEntity.setContentEncoding("UTF-8");

            httpPost.setEntity(stringEntity);

            // CloseableHttpResponse response =
            // httpclient.execute(httpPost);

            System.out.println("Executing request " + httpPost.getRequestLine());

            // Create a custom response handler
            ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
                @Override
                public String handleResponse(final HttpResponse response)
                        throws ClientProtocolException, IOException {//
                    int status = response.getStatusLine().getStatusCode();
                    if (status >= 200 && status < 300) {

                        HttpEntity entity = response.getEntity();

                        return entity != null ? EntityUtils.toString(entity) : null;
                    } else {
                        throw new ClientProtocolException(
                                "Unexpected response status: " + status);
                    }
                }
            };
            String responseBody = httpclient.execute(httpPost, responseHandler);
            System.out.println("----------------------------------------");
            System.out.println(responseBody);

        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

服务端test.php代码:

 <?php
         $result = file_get_contents('php://input');
         echo $result;
         echo json_decode(json_encode($result));

 ?>

输出结果:
{“app_name”:[“全民大讨论”],”app_ip”:[“10.21.243.234”],”app_type”:[“001”],”app_port”:[8080],”app_area”:[“asd”]}

Executing request POST http://119.29.85.118//test.php HTTP/1.1

{“app_name”:[“全民大讨论”],”app_ip”:[“10.21.243.234”],”app_type”:[“001”],”app_port”:[8080],”app_area”:[“asd”]}{“app_name”:[“全民大讨论”],”app_ip”:[“10.21.243.234”],”app_type”:[“001”],”app_port”:[8080],”app_area”:[“asd”]}

GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 个月前 )
960b763e 3 个月前
8c391e04 6 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐