美文网首页
2019-01-27 HTTP接口 GET POST请求 以及从

2019-01-27 HTTP接口 GET POST请求 以及从

作者: vpike | 来源:发表于2019-01-27 11:27 被阅读0次

post 请求 入参为Map

public static String httpPost(String url,Map<String,Object> param){
        OutputStreamWriter out=null;
        BufferedReader in=null;
        String result="";
        try {
            URL realURL=new URL(url);
            URLConnection conn=realURL.openConnection();
            // 设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent",
                 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
//            conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            conn.setConnectTimeout(180000);
            conn.setReadTimeout(180000);
            
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            out = new OutputStreamWriter(conn.getOutputStream(),"utf-8");
            // 发送请求参数
            String paramStr="";
            if(!param.isEmpty()) {
                Set<?> keySet = param.keySet();  
                for (Object key : keySet) {
                    paramStr += key+"="+param.get(key)+"&";
                }
                paramStr = paramStr.substring(0,paramStr.length()-1);  
                out.append(paramStr);
            }
           
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            
            in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream(),"UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送POST请求异常");
            e.printStackTrace();
        }
        return result;
    }

post 请求 入参为String

public static String doHttpPost(String URL, String strInfo) {
        String reStr="";
        try {
            URL url = new URL(URL);
            URLConnection con = url.openConnection();
            con.setRequestProperty("accept", "*/*");
            con.setRequestProperty("connection", "Keep-Alive");
            con.setRequestProperty("user-agent",
                 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            con.setDoOutput(true);
            con.setDoInput(true);
            OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());
            out.write(new String(strInfo.getBytes("utf-8")));
            out.flush();
            out.close();
            BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"));
            String line = "";
            for (line = br.readLine(); line != null; line = br.readLine()) {
                reStr += line;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return reStr;
    }

get 请求

public static String httpGet(String url){
        BufferedReader in=null;
        String result="";
        try {
            URL realURL=new URL(url);
            URLConnection conn=realURL.openConnection();
            // 设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent",
                 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream(),"UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送GET请求异常");
            e.printStackTrace();
        }
        return result;
    }

request 流中获取参数

    public static Map<String, String> parseRequestParameterMap(HttpServletRequest request) {
        Map<String, String> map = new HashMap<String, String>();
        Map<String, String[]> param = request.getParameterMap();
        for(Iterator<Entry<String, String[]>> itr=param.entrySet().iterator(); itr.hasNext(); ){  
            Map.Entry<String, String[]> me=(Map.Entry<String, String[]>)itr.next();
            String[] tmp = (String[]) me.getValue();
            System.out.println((String)me.getKey()+"----------"+tmp[0]);
            map.put((String)me.getKey(), tmp[0]);
        }
        return map;
    }

// 流中
public static String readRequest(HttpServletRequest request, String charset) throws IOException {
        charset = charset == null? "UTF8" : charset; 
        BufferedReader streamReader = new BufferedReader( new InputStreamReader(request.getInputStream(), charset));
        StringBuilder requestMsg = new StringBuilder();
        String inputStr;
        while ((inputStr = streamReader.readLine()) != null){
            requestMsg.append(inputStr);
        }
//      streamReader.close();
        return requestMsg.toString();
    }



相关文章

网友评论

      本文标题:2019-01-27 HTTP接口 GET POST请求 以及从

      本文链接:https://www.haomeiwen.com/subject/ukygjqtx.html