发送请求

 


 public static void main(String[] args) {

        String url = "http://localhost:8086/LoveCartoon_ZC/notify_test?a=1";
        String result = "";
        try {
            result = CommonUtil.doPostString(url, null);
            System.out.println(result);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }




        StringBuffer sb = new StringBuffer();
        sb.setLength(0);
        sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
        sb.append("<comic_ctcc>");
        sb.append("<exinfo>");
        sb.append("<c>" + 3 + "</c>");
        sb.append("</exinfo>");
        sb.append("</comic_ctcc>");

        try {
            result = CommonUtil.doPostString(url, sb.toString());
            System.out.println(result);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }


        String Json="{\"message\":\"success\",\"port\":\"1065842232\",\"result\":\"0\",\"seqId\":\"201901111199679603\"}";
        try {
            result = CommonUtil.doPostString(url, Json);
            System.out.println(result);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }




public static String doPostString(String urlMessage, String postStr) throws Exception {
        String paramStr = "";
        if (null != postStr && !postStr.isEmpty()) {
            paramStr = postStr;
        }

        URL url = new URL(urlMessage);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestProperty("accept", "*/*");
        connection.setRequestProperty("connection", "Keep-Alive");
        connection.setRequestProperty("Content-type","text/xml;charset=utf-8;application/json");
        connection.setRequestProperty("user-agent",
                "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setConnectTimeout(10000);
        connection.setReadTimeout(10000);

        DataOutputStream out = null;
        out = new DataOutputStream(connection.getOutputStream());
        out.writeBytes(paramStr);
        out.flush();

        BufferedReader in2 = null;
        String result2 = "";
        in2 = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
        String line2 = null;
        while ((line2 = in2.readLine()) != null) {
            result2 += line2;
        }
        return result2;
    }

接收

 public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.print("0");
        out.flush();
        out.close();
        System.out.println("POST 请求参数: " + JSON.toJSONString(request.getParameterMap()));
        String a = request.getParameter("a");
        String b = request.getParameter("b");
        System.out.println("POST a=" + a);
        String string = getRequestPostStr(request);
        System.out.println("POST string=" + string);
        System.out.println("=========================");
    }

    public static String getRequestPostStr(HttpServletRequest request)
            throws IOException {
        byte buffer[] = getRequestPostBytes(request);
        String charEncoding = request.getCharacterEncoding();
        if (charEncoding == null) {
            charEncoding = "UTF-8";
        }
        return new String(buffer, charEncoding);
    }

    public static byte[] getRequestPostBytes(HttpServletRequest request)
            throws IOException {
        int contentLength = request.getContentLength();
        if (contentLength < 0) {
            return null;
        }
        byte buffer[] = new byte[contentLength];
        for (int i = 0; i < contentLength; ) {

            int readlen = request.getInputStream().read(buffer, i,
                    contentLength - i);
            if (readlen == -1) {
                break;
            }
            i += readlen;
        }
        return buffer;
    }

接收结果:

post--------url-----String

 

 

相关文章: