zoukankan      html  css  js  c++  java
  • java post格式发送application/x-www-form-urlencoded

    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONObject;
    import org.apache.http.*;
    import org.apache.http.client.config.RequestConfig;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.ContentType;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.entity.mime.MultipartEntityBuilder;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.message.BasicHeader;
    import org.apache.http.protocol.HTTP;
    import org.apache.http.util.EntityUtils;
    import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
    
    import java.io.*;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.nio.charset.Charset;
    import java.nio.charset.StandardCharsets;
    import java.util.Map;
    
    public class HttpUtil {
        private final static Logger logger = LogManager.getLogger(HttpUtil.class);
    
        public static String sendPost(String url, String param) {
            String result = "";
            try {
                URL httpurl = new URL(url);
                HttpURLConnection httpConn = (HttpURLConnection) httpurl.openConnection();
                httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                httpConn.setDoOutput(true);
                httpConn.setDoInput(true);
                PrintWriter out = new PrintWriter(new OutputStreamWriter(httpConn.getOutputStream(), StandardCharsets.UTF_8));
                out.print(param);
                out.flush();
                out.close();
                BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), StandardCharsets.UTF_8));
                String line;
                while ((line = in.readLine()) != null) {
                    result += line;
                }
                in.close();
            } catch (Exception e) {
                System.out.println(e);
                logger.error(e);
            }
            return result;
        }
    }
    

      

  • 相关阅读:
    FindWindowEx使用方法
    什么是VSync
    getParameter
    高等数学积分公式大全
    为什么没有好用的Android游戏引擎?
    Best Time to Buy and Sell Stock III
    5.3 适配器模式(4.1)
    在动态网络下实现分布式共享存储
    人类智商一般在多少左右?爱因斯坦的智商是多少?
    海量数据处理面试题集锦
  • 原文地址:https://www.cnblogs.com/webttt/p/15118439.html
Copyright © 2011-2022 走看看