zoukankan      html  css  js  c++  java
  • java的post请求

    public static String sendPost(String url,Map<String, String> packageParams){
    //packageParams={channel=xajc, sign=5318a58f2125e17f164ba113c7574622}
    PrintWriter out = null;
    BufferedReader in = null;
    HttpURLConnection conn = null;
    try {
    URL realUrl = new URL(url);
    // 打开和URL之间的连接
    conn = (HttpURLConnection) realUrl.openConnection();
    // 设置通用的请求属性
    conn.setRequestProperty("accept", "*/*");
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    conn.setRequestProperty("connection", "Keep-Alive");
    conn.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
    conn.setRequestProperty("Charset", "UTF-8");
    // 发送POST请求必须设置如下两行
    conn.setDoOutput(true);
    conn.setDoInput(true);
    conn.setUseCaches(false);
    conn.setConnectTimeout(5000);
    conn.setReadTimeout(10000);
    conn.setRequestMethod("POST");

    // 获取URLConnection对象对应的输出流
    //out = new PrintWriter(conn.getOutputStream());
    out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), "utf-8"));
    StringBuffer params = new StringBuffer();
    // 表单参数与get形式一样
    for (Map.Entry<String,String> map : packageParams.entrySet()) {
    params.append(map.getKey()).append("=").append(map.getValue()).append("&");
    System.out.println(map);

    }
    params.deleteCharAt(params.lastIndexOf("&"));
    // 发送请求参数
    out.print(params.toString());
    System.out.println("请求参数: " + params.toString());
    // flush输出流的缓冲
    out.flush();
    // 定义BufferedReader输入流来读取URL的响应
    int resultCode=conn.getResponseCode();//400
    if(HttpURLConnection.HTTP_OK==resultCode){
    StringBuilder result = new StringBuilder();
    in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
    String line;
    while ((line = in.readLine()) != null) {
    result.append(line);
    }

    return result.toString();
    }
    } catch (Exception e) {
    e.fillInStackTrace();
    }finally{
    try{
    if(out!=null){
    out.close();
    }
    if(in!=null){
    in.close();
    }
    }catch(Exception ex){
    ex.printStackTrace();
    }
    }
    return "";
    }

    public static String sendPostJSON(String url, String params){
    PrintWriter out = null;
    BufferedReader in = null;
    HttpURLConnection conn = null;
    try {
    URL realUrl = new URL(url);
    // 打开和URL之间的连接
    conn = (HttpURLConnection) realUrl.openConnection();
    // 设置通用的请求属性
    conn.setRequestProperty("accept", "*/*");
    conn.setRequestProperty("Content-Type", "application/json");
    conn.setRequestProperty("connection", "Keep-Alive");
    conn.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
    conn.setRequestProperty("Charset", "UTF-8");
    // 发送POST请求必须设置如下两行
    conn.setDoOutput(true);
    conn.setDoInput(true);
    conn.setUseCaches(false);
    conn.setConnectTimeout(5000);
    conn.setReadTimeout(10000);
    conn.setRequestMethod("POST");

    // 获取URLConnection对象对应的输出流
    out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), "utf-8"));
    // 发送请求参数
    out.print(params.toString());
    // flush输出流的缓冲
    out.flush();
    // 定义BufferedReader输入流来读取URL的响应
    int resultCode=conn.getResponseCode();//400
    if(HttpURLConnection.HTTP_OK==resultCode){
    StringBuilder result = new StringBuilder();
    in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
    String line;
    while ((line = in.readLine()) != null) {
    result.append(line);
    }

    return result.toString();
    }
    } catch (Exception e) {
    e.fillInStackTrace();
    }finally{
    try{
    if(out!=null){
    out.close();
    }
    if(in!=null){
    in.close();
    }
    }catch(Exception ex){
    ex.printStackTrace();
    }
    }
    return "";
    }

  • 相关阅读:
    数据库水平切分的实现原理解析---分库,分表,主从,集群,负载均衡器
    现代 PHP 新特性系列(六) —— Zend Opcache
    Apache配置参数详解
    Git的常用命令
    PHP如何防止XSS攻击
    CSRF的防御实例(PHP)
    Web安全之CSRF攻击
    PHP五种常见的设计模式
    PHP四种基础算法详解
    php遍历文件的5种方式
  • 原文地址:https://www.cnblogs.com/helloworld-yjh/p/10751419.html
Copyright © 2011-2022 走看看