zoukankan      html  css  js  c++  java
  • Http Post 请求发送 Json数据

        public static String httpPostWithJSON(String url, String json) throws Exception {
            // 将JSON进行UTF-8编码,以便传输中文
            String encoderJson = URLEncoder.encode(json, "UTF-8");
    
            HttpClient httpclient = new HttpClient();
            PostMethod method = new PostMethod(url);
            RequestEntity requestEntity = new StringRequestEntity(encoderJson);
            method.setRequestEntity(requestEntity);
            method.addRequestHeader("Content-Type", "application/json;charset=uft-8");
    method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");//乱码问题
    int result = httpclient.executeMethod(method); String response = method.getResponseBodyAsString(); method.releaseConnection(); return response; }

    接口获取拿到需要解码

            InputStream in = req.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in, "utf-8"));
            StringBuffer sb = new StringBuffer();
            String tempStr = "";
            while ((tempStr = reader.readLine()) != null) {
                sb.append(tempStr);
            }
            System.out.println(URLDecoder.decode(sb.toString()));   //获取的Json解码一下
  • 相关阅读:
    【互联网的一些事】
    ASP.NET
    C#、ASP.NET、WinForm
    ASP.NET
    ASP.NET
    ASP.NET
    我用过的Linux命令--修改主机名
    我用过的Linux命令--关闭防火墙
    Hadoop学习笔记(2)hadoop框架解析
    Hadoop学习笔记(1)概述
  • 原文地址:https://www.cnblogs.com/dxk1019/p/9187763.html
Copyright © 2011-2022 走看看