Java原生类库发送 get post 请求

使用Java原生类库发送get post请求

  • 下面是整个工具的全部代码,其中用到了fastjson

    package util;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.Map;
    import com.alibaba.fastjson.JSON;
    
    
    public class HttpUtil {
    
    
    	public static String sendGet(String httpurl) {
    	    HttpURLConnection connection = null;
    	    InputStream is = null;
    	    BufferedReader br = null;
    	    String result = null;// 返回结果字符串
    	    try {
    	        // 创建远程url连接对象
    	        URL url = new URL(httpurl);
    	        // 通过远程url连接对象打开一个连接,强转成httpURLConnection类
    	        connection = (HttpURLConnection) url.openConnection();
    	        // 设置连接方式:get
    	        connection.setRequestMethod("GET");
    	        // 设置连接主机服务器的超时时间:15000毫秒
    	        connection.setConnectTimeout(15000);
    	        // 设置读取远程返回的数据时间:60000毫秒
    	        connection.setReadTimeout(60000);
    	        // 发送请求
    	        connection.connect();
    	        // 通过connection连接,获取输入流
    	        if (connection.getResponseCode() == 200) {
    	            is = connection.getInputStream();
    	            // 封装输入流is,并指定字符集
    	            br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
    	            // 存放数据
    	            StringBuffer sbf = new StringBuffer();
    	            String temp = null;
    	            while ((temp = br.readLine()) != null) {
    	                sbf.append(temp);
    	                sbf.append("\r\n");
    	            }
    	            result = sbf.toString();
    	        }
    	    } catch (MalformedURLException e) {
    	        e.printStackTrace();
    	    } catch (IOException e) {
    	        e.printStackTrace();
    	    } finally {
    	        // 关闭资源
    	        if (null != br) {
    	            try {
    	                br.close();
    	            } catch (IOException e) {
    	                e.printStackTrace();
    	            }
    	        }
    	
    	        if (null != is) {
    	            try {
    	                is.close();
    	            } catch (IOException e) {
    	                e.printStackTrace();
    	            }
    	        }
    	
    	        connection.disconnect();// 关闭远程连接
    	    }
    	
    	    return result;
    	}
    
    	
    	
    
    	
    	
    	
    	
    	public static String sendPost(String URL,Map<String, Object> map){
    	    OutputStreamWriter out = null;
    	    BufferedReader in = null;
    	    StringBuilder result = new StringBuilder();
    	    HttpURLConnection conn = null;
    	    try{
    	        URL url = new URL(URL);
    	        conn = (HttpURLConnection) url.openConnection();
    	        conn.setRequestMethod("POST");
    	        //发送POST请求必须设置为true
    	        conn.setDoOutput(true);
    	        conn.setDoInput(true);
    	        //设置连接超时时间和读取超时时间
    	        conn.setConnectTimeout(30000);
    	        conn.setReadTimeout(10000);
    	        conn.setRequestProperty("Content-Type", "application/json");
    	        conn.setRequestProperty("Accept", "application/json");
    	        //获取输出流
    	        out = new OutputStreamWriter(conn.getOutputStream());
    	//	            String jsonStr = "{\"qry_by\":\"name\", \"name\":\"Tim\"}";
    	        String jsonStr = JSON.toJSONString(map);
    
    	        out.write(jsonStr);
    	        out.flush();
    	        out.close();
    	        //取得输入流,并使用Reader读取
    	        if (200 == conn.getResponseCode()){
    	            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
    	            String line;
    	            while ((line = in.readLine()) != null){
    	                result.append(line);
    	            }
    	        }else{
    	            System.out.println("ResponseCode is an error code:" + conn.getResponseCode());
    	        }
    	    }catch (Exception e){
    	        e.printStackTrace();
    	    }finally {
    	        try{
    	            if(out != null){
    	                out.close();
    	            }
    	            if(in != null){
    	                in.close();
    	            }
    	        }catch (IOException ioe){
    	            ioe.printStackTrace();
    	        }
    	    }
    	    return result.toString();
    	}
    
    }
    
    
  • 在 2019年9月3日 15:50:51 测试通过,post方法中参数采用map 类型 兼容性更好,可以和spring 配置使用

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值